我读到 EditorTemplates 是自动加载的,但是从 asp.net mvc 2 到现在的 razor 3,我仍然无法让它工作。
我的模型如下所示:
public class RoleViewModel
{
public int RoleId { get; set; }
public bool InRole { get; set; }
public string RoleName { get; set; }
}
public class UserViewModel
{
public User User { get; set; }
public IEnumerable<RoleViewModel> Roles { get; set; }
}
我的观点是这样的:
〜/视图/角色/Edit.cshtml
@model Project.Web.ViewModel.UserViewModel
@using (Html.BeginForm()) {
@Html.EditorFor(model => model.Roles)
<!-- Other stuff here -->
}
〜/视图/角色/EditorTemplates/RoleViewModel.cshtml
@model Project.Web.ViewModel.RoleViewModel
@foreach (var i in Model)
{
<div>
@i.RoleName
@Html.HiddenFor(model => i.RoleId)
@Html.CheckBoxFor(model => i.InRole)
</div>
}
如果我将内容从EditorTemplate
到实际页面,然后它就可以工作,它显示复选框等。但是在当前设置中,显示的只是角色数量的计数。
我究竟做错了什么?
〜/视图/角色/EditorTemplates/RoleViewModel.cshtml
@model MvcApplication16.Controllers.RoleViewModel
<div>
@Model.RoleName
@Html.HiddenFor(m => m.RoleId)
@Html.CheckBoxFor(m => m.InRole)
</div>
〜/视图/角色/Edit.cshtml
@model MvcApplication16.Controllers.UserViewModel
@using (Html.BeginForm()) {
@Html.EditorFor(m => m.Roles)
<!-- Other stuff here -->
}
Models
public class UserViewModel {
public User User { get; set; }
public IEnumerable<RoleViewModel> Roles { get; set; }
}
public class RoleViewModel {
public int RoleId { get; set; }
public bool InRole { get; set; }
public string RoleName { get; set; }
}
public class User {
public string Name { get; set; }
}
控制器
public ActionResult Edit() {
return View(
new UserViewModel() {
User = new User() { Name = "Test" },
Roles = new List<RoleViewModel>() {
new RoleViewModel() {
RoleId = 1,
InRole = true,
RoleName = "Test Role" }}
});
}
上面的代码工作得很好。与你的比较一下,看看是否有什么问题:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)