HTML has an optgroup tag that can be used in a select element; this is supported in all major browsers.
Until now, the ASP.NET DropDownList offers no support for this. The way I see it, there are two possible solutions:
1 - Add a Group property to the ListItem class, which would be used for grouping multiple options together
2 - Add a new GroupedListItem class that inherits from ListItem and add a group property to it
The DropDownList control would need to group all items by their group text (if available) and render them all inside of a optgroup.
copied over from: https://aspnet.codeplex.com/workitem/10318
posted workaround by user: http://weblogs.asp.net/ricardoperes/archive/2013/02/02/asp-net-dropdownlist-with-groups.aspx
Comments: Potential MVC fixes would be: 1. Add a `Group` property (type `string`) to `SelectListItem`. Then our `SelectExtensions` methods could move the output `<option/>` elements within named `<optgroup/>` elements. This is similar to #1 in the bug report but uses MVC classes. 2. Add a derived `GroupedSelectListItem` class with the same `Group` property and semantics. This is similar to #2 in the bug report but uses MVC classes. 3. Add a `bool` `IsGroup` property and determine membership by order within the `selectList` – all items after each `IsGroup==true` item are in that group. Use the `Value` property to name the `<optgroup/>` and ignore the `Selected` property. Main restriction with this choice is the inability to support ungrouped options after the first group. All the above options involve new public surface but none would break existing scenarios since `Group` would default to `null` or `IsGroup` would default to `false`. Separately please note the posted workaround is for an ASP.NET scenario, not for MVC.
Until now, the ASP.NET DropDownList offers no support for this. The way I see it, there are two possible solutions:
1 - Add a Group property to the ListItem class, which would be used for grouping multiple options together
2 - Add a new GroupedListItem class that inherits from ListItem and add a group property to it
The DropDownList control would need to group all items by their group text (if available) and render them all inside of a optgroup.
copied over from: https://aspnet.codeplex.com/workitem/10318
posted workaround by user: http://weblogs.asp.net/ricardoperes/archive/2013/02/02/asp-net-dropdownlist-with-groups.aspx
Comments: Potential MVC fixes would be: 1. Add a `Group` property (type `string`) to `SelectListItem`. Then our `SelectExtensions` methods could move the output `<option/>` elements within named `<optgroup/>` elements. This is similar to #1 in the bug report but uses MVC classes. 2. Add a derived `GroupedSelectListItem` class with the same `Group` property and semantics. This is similar to #2 in the bug report but uses MVC classes. 3. Add a `bool` `IsGroup` property and determine membership by order within the `selectList` – all items after each `IsGroup==true` item are in that group. Use the `Value` property to name the `<optgroup/>` and ignore the `Selected` property. Main restriction with this choice is the inability to support ungrouped options after the first group. All the above options involve new public surface but none would break existing scenarios since `Group` would default to `null` or `IsGroup` would default to `false`. Separately please note the posted workaround is for an ASP.NET scenario, not for MVC.