2011-03-31 6 views
1

MVCのHtmlHelpersで非常に便利なものをプレイしています。今はドロップダウンリストを作成しようとしています モデルを渡すことに基づいてプロパティは値のためのものであり、テキストのものと同じものです。カスタムHtmlHelperを使用してモデルからASP.NET MVCカスタムドロップダウン

ので、私はこのようなものがあります:

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、textプロパティの名前とデフォルトの選択値

私はこれが興味を持つ人のためのコードで、以下の答えから少し助けを借りて、これを完了した:

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

のthatsそれ

多くのおかげで

+1

それはあなたの問題を解決するために十分であった場合は、答えを受け入れる必要があります。 –

+1

'タグ'クラスのコードを投稿できますか?それは図書館からですか?ありがとう – SzilardD

+0

私は "タグ"オブジェクトが存在するとは思わない - それは確かにMVC3内に存在しません..... –

答えて

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

+0

申し訳ありませんが、コードはネストされたリストを含むツリービューを作成しています。それを働かせるための助けが必要な場合は、再度投稿してください。 – rboarman

関連する問題