我刚刚学习 ASP.net MVC,所以如果我不能很好地解释我的问题,请多多包涵。
是否可以在部分 View 中使用与 View 中继承的模型不同的模型?
我的看法 Index
目前继承LoginModel
,它处理用户的授权。一旦用户获得授权,我想要 Index
显示 todos
的列表用户有。 todos
通过 LINQ 检索。
所以我的部分 View 想要继承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>
我的部分 View _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 帮助器。
您收到的错误是因为您在 Index
操作中使用 return View(todos);
将待办事项列表返回到索引页面/ View 方法。索引页面需要一个 LoginModel
对象,而不是一个 IEnumerable
待办事项对象。
<%@ 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; }
然后,修改您的索引操作方法:
[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();
}
在 View 中,执行以下操作:
<% Html.Partial("_ListTodos", Model.Todos); %>
关于ASP.net MVC4 : Using a different model in a partial view?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16697942/