我有一个列表中包含多个复选框项目的表单。我有这个列表的编辑器。它们在屏幕上显示良好。但是提交时检查的项目没有在模型中提交......
楷模:
public class ContactUsersModel : BaseModel
{
public IList<FilterCheckModel> PreviousVisitors { get; set; }
}
public class FilterCheckModel : BaseModel
{
public string Name { get; set; }
public string Value { get; set; }
public bool IsChecked { get; set; }
}
控制器:
IList<FilterCheckModel> prevVisitList = new List<FilterCheckModel>();
prevVisitList.Insert(0, new FilterCheckModel() { Name = "All previous visitors", Value = "0" });
prevVisitList.Insert(1, new FilterCheckModel() { Name = "Not visited in last month", Value = "1" });
prevVisitList.Insert(2, new FilterCheckModel() { Name = "Not visited for 1-3 months", Value = "2" });
prevVisitList.Insert(3, new FilterCheckModel() { Name = "Not visited for 3-6 months", Value = "3" });
prevVisitList.Insert(4, new FilterCheckModel() { Name = "Not visited for over 6 months", Value = "4" });
model.PreviousVisitors = prevVisitList;
html:
<table>
<tr>
<th>
Previous visitors:
</th>
</tr>
@for (int i = 1; i < Model.PreviousVisitors.Count; i++)
{
@Html.EditorFor(model => model.PreviousVisitors[i], "FilterCheck", new { Index = i + 1})
}
</table>
编辑:
<tr>
<td>
@Model.Name
</td>
<td>
@Html.CheckBoxFor(x => x.IsChecked)
@Html.HiddenFor(x => x.Value)
</td>
</tr>