我是ASP mvc的新手 我有一个像
这样的部分页面@model IEnumerable<Sample.Models.Privilege>
@{
ViewBag.Title = "Details";
}
<script type="text/javascript">
function UpdatePrivilegeSuccess() {
}
function UpdatePrivilegeFailure() {
}
</script>
<div class="settingsTable" style="position: relative; width: 100%; margin: 0 auto">
<div style="width: 50%; margin: 0 auto">
<div style="width: 50%; margin: 0 auto">
<h2>Privilege</h2>
</div>
</div>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.PrivilegeName)
</th>
<th>
@Html.DisplayNameFor(model => model.module.ModuleName)
</th>
<th>
@Html.Label("Option")
</th>
<th>Action</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.PrivilegeName)
</td>
<td>
@Html.DisplayFor(modelItem => item.module.ModuleName)
</td>
<td>
@Html.CheckBoxFor(modelItem => item.Checked)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.PrivilegeId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.PrivilegeId })
</td>
</tr>
}
</table>
@using (Ajax.BeginForm("UpdatePrivilege", "RolePrivilegemapping",
new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "PrivilegeWrapper",
OnSuccess = "UpdatePrivilegeSuccess",
OnFailure = "UpdatePrivilegeFailure"
}))
{
<p>
<input type="submit" value="Update" />
</p>
@Html.ActionLink("Update", "UpdatePrivilege", "RolePrivilegemapping")
}
</div>
我在表中列出权限。但是在用户单击“更新以更新模型”之后,“模型”在控制器操作中被接收为NULL
public ActionResult UpdatePrivilege(IEnumerable<sample.Models.Privilege> updatedPrivilege
{
return PartialView("_Privilege", One_Track.Models.DataProvider.OneTrackDataProvider.GetPtrackPrivilegeNames());
}
为什么会这样?任何帮助将不胜感激
答案 0 :(得分:1)
您至少需要移动发布到表单中的数据,或者不发布任何内容。
您还需要为集合编制索引,以便模型绑定器可以正常工作。
这是通过使用for
循环而不是foreach
完成的。
如果您需要不可编辑的字段来重新绑定,则必须将它们作为隐藏输入提供。您可以使用HiddenFor
。请在下面DisplayFor
下查看。
@using (Ajax.BeginForm("UpdatePrivilege", "RolePrivilegemapping",
new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "PrivilegeWrapper",
OnSuccess = "UpdatePrivilegeSuccess",
OnFailure = "UpdatePrivilegeFailure"
}))
{
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.PrivilegeName)
</th>
<th>
@Html.DisplayNameFor(model => model.module.ModuleName)
</th>
<th>
@Html.Label("Option")
</th>
<th>Action</th>
</tr>
@for(var i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.DisplayFor(modelItem => modelItem[0].PrivilegeName)
@Html.HiddenFor(modelItem => modelItem[0].PrivilegeName)
</td>
<td>
@Html.DisplayFor(modelItem => Model[0].ModuleName)
@Html.HiddenFor(modelItem => modelItem[0].ModuleName)
</td>
<td>
@Html.CheckBoxFor(modelItem => Model[0].Checked)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.PrivilegeId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.PrivilegeId })
</td>
</tr>
}
</table>
<p>
<input type="submit" value="Update" />
</p>
}