asp.net-mvc - 将选择列表绑定(bind)到 mvc 3 中的模型

标签 asp.net-mvc model-binding selectlist

寻求一些帮助,将选择列表绑定(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/

相关文章:

asp.net-mvc-3 - 您如何使用 DropdownList 助手正确创建 MultiSelect <select> ?

jQuery 日期选择器 : Prevent the "Today" button from being grayed out

javascript - 验证码未捕获安全错误 : Blocked a frame with origin "https://www.google.com"

asp.net-web-api - 枚举值不解析时如何将枚举作为字符串绑定(bind)失败处理

asp.net-web-api - ASP.Net Web API : Formatter Parameter Binding exception

asp.net-mvc - 我的 ASP.NET MVC SelectList 有什么问题?

c# - ASP.NET MVC 属性路由和本地化

c# - 使用 Get 将 ASP.Net MVC 中的 ViewModel 从一个 View 传递到另一个 View

asp.net-mvc-4 - ASP.NET MVC4 模型未绑定(bind)

c# - MVC DropDownList 选定的值不起作用