Optgroup drop-down support in MVC - Problems with Model Binding

后端 未结 3 1258
悲哀的现实
悲哀的现实 2021-02-08 03:58

I wonder if anyone can shed some light on this problem..

I\'ve got an option group drop-down for selecting a person\'s ethnicity – however it’s not storing the value in

3条回答
  •  被撕碎了的回忆
    2021-02-08 04:25

    This is supported natively using SelectListGroup as of ASP.NET MVC 5.2:

    var items = new List(); 
    var group1 = new SelectListGroup() { Name = "Group 1" };
    items.Add(new SelectListItem() { Text = "Item1", Group = group1 }); 
    

    Then in MVC, do

    @Html.DropDownList("select", items) 
    

提交回复
热议问题