I have read the tutorials and prepared a list of checkboxes for the page. When the form is submitted, the Selected property only get the value false. Is there something I missed? The Model
public class SelectStudentModel
{
public int StudentID { get; set; }
public string CardID { get; set; }
public string Name { get; set; }
public bool Selected { get; set;}
}
The ViewModel
public class SelectStudentViewModel
{
public List<SelectStudentModel> VMList;
public SelectStudentViewModel()
{
VMList = SelectStudentModel.GETStudent();
}
}
The View
@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 *@
The Controller for posted data
[HttpPost]
public ActionResult AddStudent(SelectStudentViewModel model)
{
foreach (SelectStudentModel m in model.VMList)
{
Console.Write(m.Selected.ToString());
}
return PartialView("StudentSelectForm", model);
}