【问题标题】:ASP.Net MVC 3: optgroup support in Html.DropDownListForASP.Net MVC 3:Html.DropDownListFor 中的 optgroup 支持
【发布时间】:2011-07-14 05:01:38
【问题描述】:

如何让我的 DropDownListFor 支持 optgroup? 有什么办法吗? 注意这里是 DropDownListFor,表示支持 DataAnnotation 客户端验证

【问题讨论】:

  • 我也在找这个,也许这对你有帮助:stackoverflow.com/questions/607188/…>

标签: asp.net-mvc-3 drop-down-menu optgroup


【解决方案1】:

在 5.2 版的 ASP.Net MVC 中添加了对 optgroups 的支持。

Group property on SelectListItem 允许您为每个项目指定一个组:

新的SelectList constructors 还允许您在提供的项目列表中提供包含组标题的字段名称。

HtmlHelper DropDownList 和 DropDownListFor 方法现在根据项目列表中包含的组生成 optgroup 元素。

【讨论】:

    【解决方案2】:

    这个看起来不错: Extending the DropDownList to show the items grouped by a category,它适用于 MVC3 和 MVC2。

    【讨论】:

    • 感谢该链接,在 MVC 4 中也对我有用(MVC 3 版本)
    【解决方案3】:

    现在支持 Dropdownlist .Net MVC 4 中的 Optgroup

    请查看HTML5 DropDownList Optgroup Tag In MVC

    @Html.DropDownGroupListFor(m => m.location_id, data, "-- Select --", new { 
        @data_val = "true",  // for Required Validation
        @data_val_required = "The Name field is required." // for Required Validation
    })
    

    Nuget Package avilable

    【讨论】:

      【解决方案4】:

      如何让我的 DropDownListFor 支持 optgroup?

      框架中没有对这种下拉列表的内置支持。您将不得不编写自己的自定义帮助程序或手动生成 HTML(我倾向于第一个选项)。

      【讨论】:

      • 我只是看看aspnet.codeplex.com 来查看DropDownListFor 的SelectListExtensions 的源代码来编写我自己的帮助程序但找不到任何帮助?你能举个例子吗?
      • @Chrno Love,你必须下载 ASP.NET MVC 3 源代码。不要在线浏览它,因为它缺少很多代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多