我有一个Form
填充了一些类似网格的结构CheckBoxes
和DisplayField
。我想用Checked CheckBoxes
. 问题是我在Controller's post
方法中得到了空值。
楷模
public class RegisterModuleSelection
{
[Display(Name = "ID")]
public int mID { get; set; }
[Display(Name = "Module")]
public string Module { get; set; }
[Display(Name = "Buy")]
public bool Buy { get; set; }
}
看法
@model IEnumerable<MAK_ERP.Models.RegisterModuleSelection>
@{
ViewBag.Title = "Register - Modules Selection";}
<h2>
Register - Modules Selection</h2>
@using (Html.BeginForm("RegisterModules", "UserAccount", FormMethod.Post, new { id = "RegisterModules", @class = "generalForm" }))
{
<table class="Grid Module">
<tr>
<th>
@Html.DisplayNameFor(model => model.Module)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th>
@Html.DisplayNameFor(model => model.Duration) (Months)
</th>
<th>
@Html.DisplayNameFor(model => model.Buy)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.HiddenFor(modelItem => item.mID)
@Html.DisplayFor(modelItem => item.Module)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.EditorFor(modelItem => item.Duration)
</td>
<td>
@Html.CheckBoxFor(modelItem => item.Buy)
</td>
</tr>
}
<tr>
<td colspan="4">
<input type="submit" value="Next" class="button3" />
<input type="reset" value="Reset" class="button4" />
</td>
</tr>
</table>
}
控制器
[HttpGet]
public ActionResult RegisterModules()
{
IEnumerable<MAK_ERP.Models.RegisterModuleSelection> model = reg.getModules();
return View(model);
}
[HttpPost]
public ActionResult RegisterModules(IEnumerable<Models.RegisterModuleSelection> regMod)
{
//regMod is null here
...