C# 无法在POST上绑定MVC模型
我正在编写一个显示经理列表的视图。经理的姓名旁边有复选框,用于选择要从经理列表中删除的经理。将表单提交绑定回视图模型时遇到问题。以下是页面的外观: 这是页面的ViewModelC# 无法在POST上绑定MVC模型,c#,asp.net-mvc-4,checkbox,C#,Asp.net Mvc 4,Checkbox,我正在编写一个显示经理列表的视图。经理的姓名旁边有复选框,用于选择要从经理列表中删除的经理。将表单提交绑定回视图模型时遇到问题。以下是页面的外观: 这是页面的ViewModel public class AddListManagersViewModel { public List<DeleteableManagerViewModel> CurrentManagers; } 这是主视图的代码: @model MyApp.UI.ViewModels.Admin.AddList
public class AddListManagersViewModel
{
public List<DeleteableManagerViewModel> CurrentManagers;
}
这是主视图的代码:
@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>
我试着跟随这篇帖子:但我肯定错过了什么
谢谢你的帮助 嗯。我认为这最终是问题所在;以下是你摆的姿势:
CurrentManagers[0].ToBeDeleted=true&CurrentManagers[0].ToBeDeleted=false&CurrentManagers[0].ExtId=X00405982144
您的模型是一个AddListManagersViewModel
,它有一个CurrentManagers
集合。因此,您发布了一个deletablemanagerviewmodel
数组,它没有绑定到“包装器”模型。可以尝试将模型参数更改为
params deletablemanagerviewmodel[]model
不过,我从来没有使用
编辑器进行扩展,所以我只是猜测…Firebug是否显示有任何发布内容?您是否尝试过添加一瞥(这将让您跟踪绑定过程)?它似乎是正确发布的:uuu RequestVerificationToken=H7L\Uq6ie_6;xaoyfhjqe2cufdjzapaf8kcu7wuvajz9adxzzzzzzzzzikmhyqylkdbvtg7cmskpqe\uz1er0ub0apxem94y1和CurrentManagers%5B0%5D。待删除=真和CurrentManagers%5B0%5B0%5D。待删除=假和CurrentManagers%5B0%5B0%5D。待删除=假和CurrentManagers%5B0%5B0%5B0%5D嗯,我能看到的唯一其他明显(可能)的问题是,当模型绑定时,如果集合的索引被破坏(跳过一个数字),最后一个序列号之后的所有内容都被忽略/丢弃。不过,我不认为您正在做的事情会有这个问题。如果我将类型更改为FormCollection,它会被填充。但我当然不想拘泥于此……如果你有时间,你应该发布一个你为解决这个问题所做的事情的例子——我相信有人会发现它很有用。你可以接受你自己的答案,因为你不需要绑定包装器模型。当我把它改为不包括包装物时,它就可以正常工作并绑定。但问题是(我认为)我需要包装器,因为页面实际上显示了两个表单(另一个要添加到管理器中,它甚至可以使用包装器工作和绑定…)
@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>
[HttpPost]
public virtual RedirectToRouteResult RemoveManagers(AddListManagersViewModel model)
{
foreach (var man in model.CurrentManagers)
{
if (man.ToBeDeleted)
{
db.Delete(man.ExtId);
}
}
return RedirectToAction("AddListManagers");
}
CurrentManagers[0].ToBeDeleted=true&CurrentManagers[0].ToBeDeleted=false&CurrentManagers[0].ExtId=X00405982144