2011-09-13 3 views
1

私は非常に短い質問があります。 は、MVCでのHiddenFieldに基づいのDropDownListを作成するには、この方法を使用して、静的な拡張メソッド式を変更する

System.Web.Mvc.Html.InputExtensions.HiddenFor(this HtmlHelper<TModel>htmlhelper,Expression<Func<TModel,TProperty>> expression,object htmlAttributes) 

I`mあります。

public static MvcHtmlString CreateDropDown<TModel, TProperty, TKey, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<ObjectData<TKey, TValue>> items, object htmlAttributes) 
    { 
      var resultVar = System.Web.Mvc.Html.InputExtensions.HiddenFor(helper, expression, htmlAttributes.ToRouteValueDictionary(new { @class = "DropDownInputHidden" })); 
      //some other code... 
      return resultVar; 
    } 

単純型プロパティの場合、そのようなHiddenFieldを作成するのは簡単です。私はこれを次のように使用します:

@Html.CreateDropDown(t=>t.SelectedValue,(some items list),(some attributes)) // t.SelectedValue is property of type string 

しかし、今私はIListインターフェイスを実装するプロパティに基づいて多くの隠しフィールドを作成したいと思います。

public static MvcHtmlString CreateDropDown<TModel, TProperty, TKey, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<ObjectData<TKey, TValue>> items, object htmlAttributes) 
    { 
      StringBuilder resultVar =new StringBuilder(); 
      for (int i = 0; i < items.Count(); i++) 
      { 
       Expression<Func<TModel,TProperty>> ExpressionThatWillPointTo_i_Element = ???; 
       //ExpressionThatWillPointTo should be "based" on expression that is "pointing" to List<string>; 
       resultVar.Append(System.Web.Mvc.Html.InputExtensions.HiddenFor(helper, ExpressionThatWillPointTo_i_Element, htmlAttributes.ToRouteValueDictionary(new { @class = "DropDownInputHidden" }))); 
      } 
      //some other code... 
      return MvcHtmlString.Create(resultVar.ToString()); 
    } 

その後私はこのようなこの修正funcion呼び出すことができていなければならない:機能は次のようになります。

@Html.CreateDropDown(t=>t.SelectedManyValues,(some items list),(some attributes)) // t.SelectedManyValuesis property of type List<string> 

をだから私は必要なものを、それぞれの値を取得するために何らかの形で式を変更することです式から。

誰かにアイデアがありますか?

+1

式ツリーを変更することはできません。これは不変な構造です。既存の –

+0

のノードにアクセスして新しい式ツリーを作成することができます。どうすればよいですか?私の問題を盛り込んだ例はありますか?それとも、別の方法がありますか? – WebDeveloper

+0

式ツリーの各ノードにアクセスする方法を教えてください。 –

答えて

0

これは、あなたが既にCreateDropDownを開発していて、そのコードを再利用したい場合があるため、私があなたの状況で行うことです。メインビューには、あなただけ行うことができ、

その後
@model System.String 

@Html.CreateDropDown(x => x, (some items list), (some attributes)) 

@Html.DisplayFor(t => t.SelectedManyValues, (some items list), (some attributes)) 

部分図を作成し、そのビューの内容のようなものでなければなりませんShared/DisplayTemplates

String.cshtmlと呼ばれます

それがあなたのために働くかどうか教えてください!