11

我想知道是否有人可以阐明这个问题..

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

视图模型

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

助手: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; }
    }
}

这是我的编辑器模板

<%@ 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")%>

但是,它没有将选定的值传递到模型中……有没有人遇到过类似的问题……非常感谢您的一些指点。

4

3 回答 3

8

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

select.GenerateId(name);
select.MergeAttribute("name", name);
于 2010-11-10T12:47:23.847 回答
5

只需更改帮助程序类以使其适用于 MVC 3 并具有可为空的 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; }
}
于 2011-07-27T07:30:07.133 回答
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) 
于 2016-06-16T15:35:57.380 回答