2011-03-31 77 views
1

我只是在MVC中玩HtmlHelpers,非常有用的東西,現在我正在嘗試創建一個基於傳入模型(任何)的下拉 什麼屬性是爲文本的值和相同。ASP.NET MVC自定義HtmlHelper模型的自定義下拉

,所以我有這樣的事情:

public static MvcHtmlString DataFilledDropDown<T>(this HtmlHelper h, string name, T selectedValue) 
    { 
     var myModel = typeof(T); 
     var dropDownBox = new Tag("select").With("name", name).And("id", name); 

     foreach (T val in Enum.GetValues(myModel)) 
     { 
      var itemText = Resources.ResourceManager.GetString(val.ToString()); 
      if (String.IsNullOrEmpty(itemText)) itemText = val.ToString(); 

      var dft = new Tag("option") 
       .With("value", "0") 
       .AndIf(val.Equals(selectedValue), "selected", "selected") 
       .WithText("-- CHOOSE --"); 
      dropDownBox.Append(dft); 

      var option = new Tag("option") 
       .With("value", (val).ToString()) 
       .AndIf(val.Equals(selectedValue), "selected", "selected") 
       .WithText(itemText); 
      dropDownBox.Append(option); 
     } 
     return MvcHtmlString.Create(dropDownBox.ToString()); 

    } 

但這僅涵蓋我要單場,但我的模型可能是這樣的:

public class TestModel{ 
    public int Id {get; set; } // this i want as a value in dropdown 
    public string Name {get;set;} // this i want to be the text value in dropdown 
    public bool isActive { get; set; } // this i dont need in dropdown but have data in model 
} 

所以上面我想,然後創建這樣的事情:

Html.DataFilledDropDown<myModel>("TestDropdown","Id","Name","0") 

其中輸入是下拉的名稱,價值的名稱p roperty,文本屬性和默認選擇的值

我已經完成了這個與下面的答案有點幫助的名字,這是任何人都感興趣的代碼:

public static MvcHtmlString DataFilledDropDown<T>(
     this HtmlHelper h, string name, IEnumerable<T> items, 
     Func<T,string> valueField, Func<T,string> textField, string selectedValue) 
    { 
     var dropDownBox = new Tag("select").With("name", name).And("id", name); 
     var defaultValue = "0"; 
     var dft = new Tag("option") 
       .With("value", "0") 
       .AndIf(defaultValue.Equals(selectedValue), "selected", "selected") 
       .WithText("-- CHOOSE --"); 
     dropDownBox.Append(dft); 

     foreach (var item in items) 
     { 
      var myValue = valueField(item); 
      var myName = textField(item); 

      var option = new Tag("option") 
       .With("value", myValue) 
       .AndIf(myValue.Equals(selectedValue), "selected", "selected") 
       .WithText(myName); 
      dropDownBox.Append(option); 

     } 

     return MvcHtmlString.Create(dropDownBox.ToString()); 
    } 

和運行代碼

<%: Html.DataFilledDropDown("SpexOptionType", Model.Clubs, x => x.clubID.ToString(), x => x.clubName, "0")%> 

完蛋了

千恩萬謝

+1

你應該接受的答案,如果這是足以讓你解決你的問題。 – 2011-04-01 10:39:04

+1

你可以發佈'標記'類的代碼嗎?它來自圖書館嗎?謝謝 – SzilardD 2011-11-23 08:43:52

+0

我不認爲「標記」對象存在 - 它肯定不存在MVC3 ..... – 2012-05-21 13:44:01

回答

1

我知道這並不直接回答你的問題,但我建議你不要使用字符串作爲你的名字和ID。他們是有問題的,並且難以保持一段時間。而是使用Func。

這是我使用的選擇列表:

public static string SelectList<T>(this HtmlHelper html, T root, IEnumerable<T> items, Func<T, string> itemValueContent, 
          Func<T, string> itemTextContent, Func<T, IEnumerable<T>> childrenProperty, Func<T, string> parentProperty, string selectSize) 
    { 
     StringBuilder parentSb = new StringBuilder(); 
     StringBuilder childSb = new StringBuilder(); 

     parentSb.AppendFormat("<select class='parent' name='parent' size='{0}'>\r\n", selectSize); 
     childSb.AppendFormat("<select class='child' id='child' size='{0}'>\r\n", selectSize); 

     foreach (T parentItem in items) 
     { 
      foreach (T item in childrenProperty(parentItem)) 
      { 
       RenderParentOption(parentSb, item, itemValueContent, itemTextContent); 

       foreach (T item1 in childrenProperty(item)) 
       { 
        RenderOptionWithClass(childSb, item1, itemValueContent, itemTextContent, parentProperty); 
       } 
      }     
     } 



     parentSb.AppendLine("</select>"); 
     childSb.AppendLine("</select>"); 


     return parentSb.ToString() + childSb.ToString(); 
    } 


    private static void RenderOptionWithClass<T>(StringBuilder sb, T item, Func<T, string> itemValueContent, Func<T, string> itemTextContent, Func<T, string> parentProperty) 
    { 
     sb.AppendFormat("<option class='sub_{2}' value='{0}'>{1}</option>\r\n", itemValueContent(item), itemTextContent(item), parentProperty(item)); 
    } 

    private static void RenderParentOption<T>(StringBuilder sb, T item, Func<T, string> itemValueContent, Func<T, string> itemTextContent) 
    { 
     sb.AppendFormat("<option value='{0}'>{1}</option>\r\n", itemValueContent(item), itemTextContent(item) + " ->"); 
    } 

這是如何使用它:

<%=Html.SelectList<HierarchyNode<CategoryModel>>(Model.CategoryList.First(), Model.CategoryList, 
     x => x.Entity.RowId.ToString(), 
     x => x.Entity.Name.ToString(), 
     x => x.ChildNodes, 
     x => x.Entity.ParentCategoryId.ToString(), "10")%> 
+0

謝謝我不知道你在哪裏似乎創建2下拉列表它似乎有點混亂,但我已經採取了有關Func <>的筆記,並用它來修改我的版本以實際工作,所以謝謝你的提示。如果你想看,我已經修改了我的帖子以顯示解決方案。 ta – davethecoder 2011-04-01 08:57:16

+0

不好意思,代碼是用嵌套列表創建一個樹形視圖。如果您需要一些幫助才能使其工作,請再次發佈。 – rboarman 2011-04-01 17:07:04