ASP.Net MVC 3: optgroup support in Html.DropDownListFor
Asked Answered
F

4

9

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

Ferruginous answered 14/7, 2011 at 5:1 Comment(1)
I looking for this too and maybe this will help U: <#607688>Pannell
S
4

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).

Scansion answered 14/7, 2011 at 8:33 Comment(2)
I just take a look into aspnet.codeplex.com to see the source code of SelectListExtensions for DropDownListFor to write my own helper but couldn't find any? can you make some example plzzz?Ferruginous
@Chrno Love, you have to download the ASP.NET MVC 3 source code. Don't browse it online as it is missing lots of code.Scansion
S
12

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.

Silici answered 9/9, 2014 at 13:42 Comment(0)
P
6

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

Penitent answered 16/12, 2011 at 7:50 Comment(1)
Thanks for that link, works great for me in MVC 4 as well (the MVC 3 version)Inweave
R
5

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

Resor answered 27/1, 2014 at 17:18 Comment(0)
S
4

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).

Scansion answered 14/7, 2011 at 8:33 Comment(2)
I just take a look into aspnet.codeplex.com to see the source code of SelectListExtensions for DropDownListFor to write my own helper but couldn't find any? can you make some example plzzz?Ferruginous
@Chrno Love, you have to download the ASP.NET MVC 3 source code. Don't browse it online as it is missing lots of code.Scansion

© 2022 - 2024 — McMap. All rights reserved.