Asp.net

ASP.net MVC4:在局部視圖中使用不同的模型?

  • November 17, 2013

我只是在學習 ASP.net MVC,所以如果我不擅長解釋我的問題,請多多包涵。

是否可以在局部視圖中使用與視圖中繼承的模型不同的模型?

我的視圖Index目前繼承LoginModel,它處理使用者的授權。一旦使用者被授權,我希望Index顯示todos使用者擁有的列表。todos通過 LINQ 檢索。

所以我的部分視圖想要繼承System.Web.Mvc.ViewPage<IEnumerable<todo_moble_oauth.Models.todo>>,但是當我使用它時出現錯誤:`傳遞到字典中的模型項是類型

System.Data.Linq.DataQuery`1[todo_moble_oauth.Models.todo]', but this dictionary requires a model item of type 'todo_moble_oauth.Models.LoginModel'

這是我的Index看法

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<todo_moble_oauth.Models.LoginModel>" %>

<section id="loginForm">
   <% if (Request.IsAuthenticated) { %>

       <% Html.RenderPartial("_ListTodos"); %>

   <% } else { %>

       <h1>Todo Mobile</h1>

       <blockquote>Easily store your list of todos using this simple mobile application</blockquote>

       <% using (Html.BeginForm()) { %>
           <%: Html.AntiForgeryToken() %>
           <%: Html.ValidationSummary(true) %>

                   <%: Html.LabelFor(m => m.UserName) %>
                   <p class="validation"><%: Html.ValidationMessageFor(m => m.UserName) %></p>
                   <%: Html.TextBoxFor(m => m.UserName) %>

                   <%: Html.LabelFor(m => m.Password) %>
                   <p class="validation"><%: Html.ValidationMessageFor(m => m.Password) %></p>
                   <%: Html.PasswordFor(m => m.Password) %>

                   <label class="checkbox" for="RememberMe">
                       <%: Html.CheckBoxFor(m => m.RememberMe) %>
                       Remember Me?
                   </label>

           <input type="submit" value="Login" />
       <% } %>
   <% } %>
</section>

我的部分觀點_ListTodos如下:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<todo_moble_oauth.Models.todo>>" %>

<% foreach (var item in Model) { %>
     <%: Html.DisplayFor(modelItem => item.title) %>
     <%: Html.DisplayFor(modelItem => item.description) %>
<% } %>

LoginModel的有以下幾點:

public class LoginModel
{
   [Required]
   [Display(Name = "User name")]
   public string UserName { get; set; }

   [Required]
   [DataType(DataType.Password)]
   [Display(Name = "Password")]
   public string Password { get; set; }

   [Display(Name = "Remember me?")]
   public bool RememberMe { get; set; }
}

HomeController Index()方法:

   [AllowAnonymous]
   public ActionResult Index()
   {
       // if user is logged in, show todo list
       if (Request.IsAuthenticated)
       {
           //var currentUser = Membership.GetUser().ProviderUserKey;
           todosDataContext objLinq = new todosDataContext();
           var todos = objLinq.todos.Select(x => x);
           return View(todos);
       }
       return View();
   }

非常感謝任何幫助,謝謝。

當然你可以這樣做:

<% Html.Partial("_ListTodos", userTodos); %>

userTodos作為參數傳遞給 Partial 助手。

您收到的錯誤是因為您在 action 方法中將待辦事項列表返回到索引頁面/return View(todos);視圖Index。索引頁面需要一個LoginModel對象而不是IEnumerabletodo 對象。

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
   Inherits="System.Web.Mvc.ViewPage<todo_moble_oauth.Models.LoginModel>" %>

為了解決這個問題,你需要改變你通過的方式todos。由於您的Index頁面收到一個,您可以像這樣向此類LoginModel添加一個屬性:Todos

[Required]
[Display(Name = "User name")]
public string UserName { get; set; }

[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }

[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }

public IEnumerable<todo_moble_oauth.Models.todo> Todos { get; set; }

然後,修改您的 Index 操作方法:

[AllowAnonymous]
public ActionResult Index()
{
   // if user is logged in, show todo list
   if (Request.IsAuthenticated)
   {
       //var currentUser = Membership.GetUser().ProviderUserKey;
       todosDataContext objLinq = new todosDataContext();
       var todos = objLinq.todos.Select(x => x);

       LoginModel model = new LoginModel();
       model.Todos = todos;

       return View(model);
   }

   return View();
}

在視圖中,執行以下操作:

<% Html.Partial("_ListTodos", Model.Todos); %>

引用自:https://stackoverflow.com/questions/16697942