ASP.Net MVC 3: optgroup support in Html.DropDownListFor

ⅰ亾dé卋堺 提交于 2019-12-10 02:23:41

问题


How can i make my DropDownListFor support optgroup? Is there anyway to do it? Notice that this is DropDownListFor, means that it support DataAnnotation client validation


回答1:


How can i make my DropDownListFor support optgroup?

There is no built-in support in the framework for this kind of drop down lists. You will have to write your own custom helper or generate the HTML manually (I would tend towards the first option).




回答2:


Support for optgroups was added to ASP.Net MVC at version 5.2.

The Group property on SelectListItem allows you to specify a group for each item:

New SelectList constructors also allow you to provide the name of the field that contains the group title on the supplied list of items.

The HtmlHelper DropDownList and DropDownListFor methods now generate optgroup elements based on the groups included on the list of items.




回答3:


This one seems nice: Extending the DropDownList to show the items grouped by a category, and it works for both MVC3 and MVC2.




回答4:


Now Support For Optgroup In Dropdownlist .Net MVC 4

Please Check 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



来源:https://stackoverflow.com/questions/6688682/asp-net-mvc-3-optgroup-support-in-html-dropdownlistfor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!