寻求一些帮助,将选择列表绑定(bind)到表单提交上的模型。这一切都有效
@Html.DropDownListFor(m => m.LectureRoomId, new SelectList(Model.LectureRooms, "Id", "Name"), new { @class = "input-small chosen-select"})
但是我们正在使用 selected.js 并希望使用 optgroup 对下拉项进行分组。据我了解,直到 DropDownListFor 中的 mvc 4 或 5 才支持 optgroup,因此我必须正确选择自定义选择
<select class="chosen-select" id="venues" >
@foreach (var group in Model.Campuses.Distinct())
{
<optgroup label="@group.Name">
@foreach (var item in Model.LectureRooms.Where(r => r.CampusId == group.Id))
{
<option value="@item.Id" width="120px">@item.Name</option>
}
</optgroup>
}
</select>
此代码的工作原理是列表已填充且组均正确显示。但是,在提交表单时,我不知道列表将其所选项目绑定(bind)到什么模型对象(如果有)。您能否协助解释我如何将所选项目列表绑定(bind)到模型,以便我可以在我的 Controller 中使用它。
谢谢
最佳答案
您的选择没有 name 属性,因此不会提交。将行更改为:
<select class="chosen-select" id="venues" name="@Html.NameFor(m => m.LectureRoomId)">
但是,NameFor
帮助程序仅在 MVC 4 中可用,但您可以按照 this answer 中所述实现自己的帮助程序。 .
关于asp.net-mvc - 将选择列表绑定(bind)到 mvc 3 中的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28473974/