Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to bind dynamic dropdownlist items group wise in MVC. My dropdown is simply bind with all list item but I want to achieve dropdownlist similar as "optgroup" html select option. Any one can help me

What I have tried:

This is my html page :
<pre>@Html.DropDownList("cProductionLines", ViewBag.cProductionLines as SelectList,  @Resources.TitleResource.ProductionLines, new { @class = "form-control", @id = "cProductionLines" })


and this is my controller code:

private void bindRequestProductionLines()
        {
            List<COP_ProductList> ProductList = OUnitOfWork.ProductListRepositories.FindBy(x => x.IsActive == true).ToList();
            var ProductionLinesList = ProductList.Select(x => new

            {
                Id = x.Id,
                Type = !CorporateSite.Helpers.CultureHelper.IsLanguageArabic ? x.Title_En : x.Title_Ar,
                Category = !CorporateSite.Helpers.CultureHelper.IsLanguageArabic ? x.COP_ProductionLines.Name_EN : x.COP_ProductionLines.Name_AR,
                CategoryId = x.COP_ProductionLines_Id
            }).ToList();

            ViewBag.cProductionLines = new SelectList(ProductionLinesList, "Id", "Type", "Category");
            
        }
Posted
Updated 25-Aug-17 2:19am

1 solution

This article should help: HTML “optgroup” support in DropDownList – ASP.NET MVC 5.2 | .NET Rumbles[^]

TL;DR ... start with the basics:
HTML
@Html.DropDownList("cProductionLines")

Now, if that is working fine, you can add your attributes:
HTML
@Html.DropDownList("cProductionLines", null, new { @class = "form-control", @id = "cProductionLines" })
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900