[英]Unable to Bind MVC Model on POST
我正在编写一个显示经理列表的视图。 管理员在其名称旁边有复选框,以选择将其从经理列表中删除。 我在将表单提交绑定回我的视图模型时遇到问题。 这是页面的样子:
这是页面的ViewModel。
public class AddListManagersViewModel
{
public List<DeleteableManagerViewModel> CurrentManagers;
}
这是每个DeleteableManagers的子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多个选择:模型中的难度绑定但我必须遗漏一些东西....
谢谢你的帮助!
嗯。 我认为这最终是问题; 这是你在摆姿势:
CurrentManagers[0].ToBeDeleted=true&CurrentManagers[0].ToBeDeleted=false&CurrentManagers[0].ExtId=X00405982144
您的模型是一个AddListManagersViewModel
,它AddListManagersViewModel
CurrentManagers
。 所以,你发布了一个DeleteableManagerViewModel
数组,它没有被绑定到“包装器”模型。 您可以尝试将模型参数更改为
params DeleteableManagerViewModel[] model
我不会使用EditorFor
扩展,所以我只是在猜...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.