ASP.net MVC4:在局部视图中使用不同的模型?
|
我只是在学习ASP.net MVC,所以如果我不善于解释我的问题,请耐心等待. 是否可以在局部视图中使用与视图中继承的模型不同的模型? 我的观点Index目前继承了LoginModel,它处理用户的授权.一旦用户被授权,我希望索引显示用户拥有的待办事项列表.通过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' 这是我的索引视图 <%@ 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 helper. 你得到的错误是因为你将一个todos列表返回到Index页面/视图,返回View(todos);在Index动作方法中. Index页面需要一个LoginModel对象而不是IEnumerable的todo对象. <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<todo_moble_oauth.Models.LoginModel>" %>
要解决这个问题,你需要改变传递todos的方式.由于您的索引页面收到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();
}
在视图中,执行以下操作: <% Html.Partial("_ListTodos",Model.Todos); %> (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 为什么aspx代码隐藏文件被声明为部分类?
- Asp.Net会话在ashx文件中为null
- 为什么asp.net将页面包装在一个表单中?
- asp.net – 任何人都有解决Internet上的“n项剩余”问题的想
- asp.net-mvc – 使用@ Html.DisplayNameFor()与PagedList
- asp.net – 将tracelistener添加到web.config
- asp.net – Html.RenderAction和Html.Action之间的区别
- asp.net-mvc – 在asp.net mvc 3剃刀中识别html助手的问题
- asp.net-mvc-3 – MVC3 AllowHtml属性的问题
- asp.net-mvc – 如何实现在jqgrid搜索?
- asp.net – .NET Web API HttpResponseMessage模
- asp.net – 限制关于AJAX调用的视图状态信息
- asp.net – IControllerFactory’MyWebSite.WebU
- 在ASP和ASP.Net之间共享登录系统
- “DataSource和DataSourceID都被定义为”使用ASP
- asp.net-mvc-3 – 模型验证/ ASP.NET MVC 3 – 条
- asp.net-mvc-2 – 如何在ASP.NET MVC2中枚举form
- asp.net – 内容控件必须是顶级控件
- asp.net-mvc – 找到相同类型的两个实体之间的差
- asp.net-mvc-4 – 具有动态部分视图创建的MVC Aj
