How can I add option groups in ASP.NET drop down list?

后端 未结 10 1178
遇见更好的自我
遇见更好的自我 2020-11-28 10:22

I have a requirement of grouping the drop down list options in ASP.NET drop down server control. Do you have any idea to how to approach the issue? I am new to ASP.NET.

10条回答
  •  爱一瞬间的悲伤
    2020-11-28 10:52

    ----- in .cs  -----
    
    List sl = new List();
    
    sl.Add(new SelectListItem() { Text = "My text", Value = "1", Group = new SelectListGroup() { Name = "First Group" } });
    
    sl.Add(new SelectListItem() { Text = "My text 2", Value = "2", Group = new SelectListGroup() { Name = "First Group" } });
    
    var sl1 = new SelectList(sl,"Value","Text","Group.Name",-1);
    
    ViewData["MyList"] = sl1;
    
    
    ----- in .cshtml    -----
    Html.DropDownList("My List", ViewData["MyList"] as SelectList,
                            "-- No Preference --",
                            new {
                                @class = "ui-widget ui-corner-all square_corners searchPageDropdown"
                            }))
    

提交回复
热议问题