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

Hieu Nguyen Trung picture Hieu Nguyen Trung · Jul 14, 2011 · Viewed 13.8k times · Source

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

Answer

Dan Malcolm picture Dan Malcolm · Sep 9, 2014

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.