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

后端 未结 3 1259
悲哀的现实
悲哀的现实 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:21

    Your select doesn't have a name attribute and so when you submit the form the selected value is not sent to the server. You need to add a name:

    select.GenerateId(name);
    select.MergeAttribute("name", name);
    
    0 讨论(0)
  • 2021-02-08 04:25

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

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

    Then in MVC, do

    @Html.DropDownList("select", items) 
    
    0 讨论(0)
  • 2021-02-08 04:44

    Just changed the helper class to get it work for MVC 3 and with nullable int. Thanks a lot for the class, saves me plenty of time.

    public static class GroupDropListExtensions
    {
        public static MvcHtmlString GroupDropList(this HtmlHelper helper, string name, IEnumerable<GroupDropListItem> data, int? SelectedValue, object htmlAttributes)
        {
            if (data == null && helper.ViewData != null)
                data = helper.ViewData.Eval(name) as IEnumerable<GroupDropListItem>;
            if (data == null) return new MvcHtmlString(string.Empty);
    
            var select = new TagBuilder("select");
    
            if (htmlAttributes != null)
                select.MergeAttributes(new RouteValueDictionary(htmlAttributes));
    
            select.GenerateId(name);
            select.MergeAttribute("name", name);
    
            var optgroupHtml = new StringBuilder();
            var groups = data.ToList();
            foreach (var group in data)
            {
                var groupTag = new TagBuilder("optgroup");
                groupTag.Attributes.Add("label", helper.Encode(group.Name));
                var optHtml = new StringBuilder();
                foreach (var item in group.Items)
                {
                    var option = new TagBuilder("option");
                    option.Attributes.Add("value", helper.Encode(item.Value));
                    if (SelectedValue != 0 && item.Value == SelectedValue)
                        option.Attributes.Add("selected", "selected");
                    option.InnerHtml = helper.Encode(item.Text);
                    optHtml.AppendLine(option.ToString(TagRenderMode.Normal));
                }
                groupTag.InnerHtml = optHtml.ToString();
                optgroupHtml.AppendLine(groupTag.ToString(TagRenderMode.Normal));
            }
            select.InnerHtml = optgroupHtml.ToString();
            return new MvcHtmlString(select.ToString(TagRenderMode.Normal));
        }
    }
    
    public class GroupDropListItem
    {
        public string Name { get; set; }
        public List<OptionItem> Items { get; set; }
    }
    
    public class OptionItem
    {
        public string Text { get; set; }
        public int Value { get; set; }
    }
    
    0 讨论(0)
提交回复
热议问题