您应该使用强类型编辑器将结果传递给控制器(模型绑定器).
我更喜欢这样做.
模型
public class YourViewModel { public ListStudents { get; set; } }
控制器获取
Students= service.GetStudents(); //Fill the list
视图
@for (var i = 0; i < Model.Students.Count; i++)
{
@Html.CheckBoxFor(m => m.Students[i].Selected)
@Html.HiddenFor(m => m.Students[i].Text)
@Html.HiddenFor(m => m.Students[i].Value)
@Model.Students[i].Text
}
控制器帖子
[HttpPost] public ActionResult Create(YourViewModel model) { foreach(var student in model.Students) { if(student.Selected) { // Do your logic} } }
或者, 您可以使用数组或字符串列表.在此示例中使用ListBox.
public string[] SelectedStudents{ get; set; } @Html.ListBoxFor(s => s.SelectedStudents, new MultiSelectList(Model.Students, "Value", "Text", Model.SelectedStudents), new { @class = "form-control", style = "height:250px; width:100%" })