【发布时间】:2016-03-08 05:32:28
【问题描述】:
我已阅读教程并为该页面准备了一个复选框列表。提交表单时,Selected 属性仅获取值 false。 有什么我错过的吗? 模型
public class SelectStudentModel
{
public int StudentID { get; set; }
public string CardID { get; set; }
public string Name { get; set; }
public bool Selected { get; set;}
}
视图模型
public class SelectStudentViewModel
{
public List<SelectStudentModel> VMList;
public SelectStudentViewModel()
{
VMList = SelectStudentModel.GETStudent();
}
}
观点
@using Student.Models
@model SelectStudentViewModel
@using (Html.BeginForm("AddStudent", "SectionStudent", FormMethod.Post, new { @role = "form" }))
{
@{ for (int i = 0; i < Model.VMList.Count(); i++)
{
<tr>
<td>@Html.CheckBoxFor(m => m.VMList[i].Selected)</td>
<td>@Html.DisplayFor(model => model.VMList[i].Name)</td>
</tr>
}
}
<input type="submit" value="submit" />
}@* end form *@
发布数据的控制器
[HttpPost]
public ActionResult AddStudent(SelectStudentViewModel model)
{
foreach (SelectStudentModel m in model.VMList)
{
Console.Write(m.Selected.ToString());
}
return PartialView("StudentSelectForm", model);
}
【问题讨论】:
-
您似乎向我们展示了错误的模型视图中的模型是
SelectStudentViewModel,但您显示的唯一模型是SelectStudentModel