MVC中的Optgroup下拉支持 - 模型绑定问题

时间:2010-11-10 09:33:48

标签: asp.net-mvc-2 drop-down-menu optgroup

我想知道是否有人可以解决这个问题。

我有一个选项组下拉列表用于选择一个人的种族 - 但是它没有将该值存储在模型中。

视图模型

    [UIHint("EthnicOriginEditorTemplate")]
    [DisplayName("Question 6: Ethnic Origin")]
    public int EthnicOrigin { get; set; }

Helper:GroupDropList.Cs

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace Public.Helpers
{
    public static class GroupDropListExtensions
    {
        public static string 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 string.Empty;

            var select = new TagBuilder("select");

            if (htmlAttributes != null)
                select.MergeAttributes(new RouteValueDictionary(htmlAttributes));

            select.GenerateId(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 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; }
    }
}

这是我的EditorTemplate

<%@ Import Namespace="Public.Helpers"%>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<int>"%>

<%=Html.GroupDropList("EthnicOrigin",
                                 new[]
                                    {                                        
                                        new GroupDropListItem
                                             {
                                                 Name = "Ethnicity",
                                                 Items = new List<OptionItem>
                                                         {
                                                             new OptionItem {Value = 0, Text = "Please Select"}
                                                         }
                                             },

                                        new GroupDropListItem
                                             {
                                                 Name = "a) White",
                                                 Items = new List<OptionItem>
                                                         {
                                                             new OptionItem {Value = 1, Text = "British"},
                                                             new OptionItem {Value = 2, Text = "Irish"},
                                                             new OptionItem {Value = 3, Text = "Other White (Please specify below)"}
                                                         }
                                             },

                                         --snip

                                     }, Model, null)%>

在视图中我将其引用为:

<%=Html.EditorFor(x => x.EthnicOrigin, "EthnicOriginEditorTemplate")%>

然而,它没有通过选定的值传递到模型中...有任何人遇到过类似的问题......非常感谢提前做了一些指示。

3 个答案:

答案 0 :(得分:8)

您的select没有name属性,因此当您提交表单时,所选值不会发送到服务器。您需要添加名称:

select.GenerateId(name);
select.MergeAttribute("name", name);

答案 1 :(得分:5)

刚刚更改了助手类,使其适用于MVC 3和nullable int。 非常感谢上课,节省了我很多时间。

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; }
}

答案 2 :(得分:1)

从ASP.NET MVC 5.2开始,本机支持使用SelectListGroup:

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

然后在MVC中,执行

@Html.DropDownList("select", items)