我有以下型号:
select
遵循GET方法:
public class ProjectMemberUserRolesViewModel
{
public List<ProjectMemberUserRolesElementViewModel> Elements { get; set; }
}
public class ProjectMemberUserRolesElementViewModel
{
public string AccessType { get; set; }
public bool Delete { get; set; }
public bool Create { get; set; }
public bool Edit { get; set; }
public bool Read { get; set; }
}
查看型号:
public ActionResult UserRoles(int id)
{
ProjectMemberUserRolesViewModel model = new ProjectMemberUserRolesViewModel();
model.Elements = (from i in db.ProjectAccessTypes
select new ProjectMemberUserRolesElementViewModel()
{
AccessType = i.Type,
Create = i.ProjectMemberAccess.Where(p => p.ProjectMemberID == id).Select(p => p.Create).FirstOrDefault(),
Delete = i.ProjectMemberAccess.Where(p => p.ProjectMemberID == id).Select(p => p.Delete).FirstOrDefault(),
Edit = i.ProjectMemberAccess.Where(p => p.ProjectMemberID == id).Select(p => p.Edit).FirstOrDefault(),
Read = i.ProjectMemberAccess.Where(p => p.ProjectMemberID == id).Select(p => p.Read).FirstOrDefault()
}).ToList();
return View(model);
}
和POST方法
@model ProjectMemberUserRolesViewModel
@{
ViewBag.Title = "UserRoles";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>UserRoles</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
foreach (var item in Model.Elements)
{
<div>
@Html.DisplayFor(modelItem => item.AccessType)
</div>
<div>
@Html.CheckBoxFor(modelItem => item.Delete) @Html.DisplayNameFor(modelItem => item.Delete)
@Html.CheckBoxFor(modelItem => item.Create) @Html.DisplayNameFor(modelItem => item.Create)
@Html.CheckBoxFor(modelItem => item.Edit) @Html.DisplayNameFor(modelItem => item.Edit)
@Html.CheckBoxFor(modelItem => item.Read) @Html.DisplayNameFor(modelItem => item.Read)
</div>
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
}
问题是对于POST方法,ProjectMemberUserRolesViewModel模型参数为null。如果我查看页面源代码,我看到,foreach元素没有前缀:
[HttpPost]
public ActionResult UserRoles(ProjectMemberUserRolesViewModel model)
{
//........
return View(model);
}
如何正确查看代码?
答案 0 :(得分:4)
如何正确查看代码?
将foreach
循环替换为for
:
for (var i = 0; i < Model.Elements.Count; i++)
{
<div>
@Html.DisplayFor(x => x.Elements[i].AccessType)
</div>
<div>
@Html.CheckBoxFor(x => x.Elements[i].Delete)
@Html.DisplayNameFor(x => x.Elements[i].Delete)
@Html.CheckBoxFor(x => x.Elements[i].Create)
@Html.DisplayNameFor(x => x.Elements[i].Create)
@Html.CheckBoxFor(x => x.Elements[i].Edit)
@Html.DisplayNameFor(x => x.Elements[i].Edit)
@Html.CheckBoxFor(x => x.Elements[i].Read)
@Html.DisplayNameFor(x => x.Elements[i].Read)
</div>
}
或使用编辑器模板:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.EditorFor(x => x.Elements)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
}
然后在相应的编辑器模板内部,它将由ASP.NET MVC为集合~/Views/Shared/EditorTemplates/ProjectMemberUserRolesElementViewModel.cshtml
的每个元素自动呈现:
@model ProjectMemberUserRolesElementViewModel
<div>
@Html.DisplayFor(x => x.AccessType)
</div>
<div>
@Html.CheckBoxFor(x => x.Delete)
@Html.DisplayNameFor(x => x.Delete)
@Html.CheckBoxFor(x => x.Create)
@Html.DisplayNameFor(x => x.Create)
@Html.CheckBoxFor(x => x.Edit)
@Html.DisplayNameFor(x => x.Edit)
@Html.CheckBoxFor(x => x.Read)
@Html.DisplayNameFor(x => x.Read)
</div>