在我的 MVC4 项目视图中,我将IEnumerable<Settings>
模型传递给视图。当我将表单提交回我的控制器时,控制器方法的参数为IEnumerable<Settings> data
. 问题是它data
始终为空。
看法
@model IEnumerable<Settings>
@using (Html.BeginForm("Index", "Layout", FormMethod.Post)))
{
@foreach (var item in Model)
{
@Html.CheckBoxFor(modelItem => item.IsActive, new { @value = item.Id })
@Html.DisplayFor(modelItem => item.Name)
}
<input type="submit" value="Save" />
}
控制器
[HttpPost]
public ActionResult Index(IEnumerable<Settings> data)
{
--actual code goes here
}
模型
[Serializable]
public class Settings
{
public virtual byte Id { get; set; }
public virtual string Name { get; set; }
public virtual bool IsActive { get; set; }
public virtual DataTable DataResult { get; set; }
public virtual int CreatedBy { get; set; }
public virtual DateTime CreatedDate { get; set; }
public virtual int ClientId { get; set; }
public virtual Int16 NoofItems { get; set; }
}
更新: *呈现的 HTML *
<td>
<input checked="checked" data-val="true" data-val-required="The IsActive field is required." id="item_IsActive" name="item.IsActive" type="checkbox" value="13" class="valid"><input name="item.IsActive" type="hidden" value="false">
# of pieces
</td>
更新 1: 在安德烈回答后呈现 HTML
<td>
<input checked="checked" data-val="true" data-val-required="The IsActive field is required." name="[0].IsActive" type="checkbox" value="13"><input name="[0].IsActive" type="hidden" value="false">
# of pieces</td>