Asp.net-Mvc-2

MVC 中的 Optgroup 下拉支持 - 模型綁定問題

  • June 16, 2016

我想知道是否有人可以闡明這個問題..

我有一個用於選擇一個人的種族的選項組下拉列表——但是它沒有將值儲存在模型中。

視圖模型

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

但是,它沒有將選定的值傳遞到模型中…有沒有人遇到過類似的問題…在此先感謝您的一些指示。

select沒有name屬性,因此當您送出表單時,所選值不會發送到伺服器。您需要添加一個名稱:

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

只需更改幫助程序類以使其適用於 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; }
}

引用自:https://stackoverflow.com/questions/4142986