在我的asp.net mvc4应用程序中,我使用复选框选择Ilist of objects并将它们发送给控制器。我在数据库(SelectedInterviewer)中声明了新的布尔非可空变量,它控制是否选择了对象。问题是这个布尔变量总是为假。请帮忙。
这是我的代码的一部分:
视图:
@model IList<PFEApplication.Models.agent>
<div id="Interviewer">
@using (Ajax.BeginForm("AddNewInterviewer", "Equipe", FormMethod.Post, new AjaxOptions
{
UpdateTargetId = "Interviewer",
HttpMethod = "Post",
InsertionMode = InsertionMode.Replace
}))
{
for (int i = 0; i < Model.Count(); i++)
{
@Html.HiddenFor(modelItem => modelItem[i].ID_agent)
@Html.CheckBoxFor(modelItem => Model[i].SelectedInterviewer)
@Html.DisplayFor(modelItem => modelItem[i].nom_agent) @Html.Raw(" ")
@Html.DisplayFor(modelItem => modelItem[i].prenom_agent)
<br />
}
<br />
<input type="submit" value="Save" />
}
</div>
控制器:
[HttpPost]
public ActionResult AddNewInterviewer(IEnumerable<agent> ListAgent)
{
[...]
if (ListAgent != null)
foreach (agent agent in ListAgent)
{
if (agent.SelectedInterviewer)
{
agent.ID_equipe = Id;
}
}
db.SaveChanges();
return PartialView("exitAddNewInterviewer");
}