私は非常に短い質問があります。 は、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>
をだから私は必要なものを、それぞれの値を取得するために何らかの形で式を変更することです式から。
誰かにアイデアがありますか?
式ツリーを変更することはできません。これは不変な構造です。既存の –
のノードにアクセスして新しい式ツリーを作成することができます。どうすればよいですか?私の問題を盛り込んだ例はありますか?それとも、別の方法がありますか? – WebDeveloper
式ツリーの各ノードにアクセスする方法を教えてください。 –