我正在编写一个显示经理列表的视图。经理在他们的名字旁边有复选框来选择他们从经理列表中删除。我在将表单提交绑定回我的视图模型时遇到问题。页面如下所示:
这是页面的 ViewModel。
public class AddListManagersViewModel
{
public List<DeleteableManagerViewModel> CurrentManagers;
}
这是每个 DeleteableManager 的子 ViewModel:
public class DeleteableManagerViewModel
{
public string ExtId { get; set; }
public string DisplayName { get; set; }
public bool ToBeDeleted { get; set; }
}
这是主视图的代码:
@model MyApp.UI.ViewModels.Admin.AddListManagersViewModel
<div class="row">
<div class="span7">
@using (Html.BeginForm("RemoveManagers","Admin"))
{
@Html.AntiForgeryToken()
<fieldset>
<legend>System Managers</legend>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Remove</th>
</tr>
</thead>
<tbody>
@Html.EditorFor(model => model.CurrentManagers)
</tbody>
</table>
</fieldset>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Delete Selected</button>
</div>
}
</div>
</div>
这是我为 DeleteableManagerViewModel 创建的 EditorTemplate:
@model MyApp.UI.ViewModels.Admin.DeleteableManagerViewModel
<tr>
<td>@Html.DisplayFor(model => model.DisplayName)</td>
<td>
@Html.CheckBoxFor(model => model.ToBeDeleted)
@Html.HiddenFor(model => model.ExtId)
</td>
</tr>
但是当我将表单提交给控制器时,模型会返回 null!这就是我想要它做的:
[HttpPost]
public virtual RedirectToRouteResult RemoveManagers(AddListManagersViewModel model)
{
foreach (var man in model.CurrentManagers)
{
if (man.ToBeDeleted)
{
db.Delete(man.ExtId);
}
}
return RedirectToAction("AddListManagers");
}
我尝试遵循这篇文章:CheckBoxList 多项选择:模型绑定困难,但我必须遗漏一些东西....
谢谢你的帮助!