5

リクエスト パラメータから取得した値でバインディング プレフィックスを変更する方法はありますか?

ネストされた検索ポップアップが多数あり、それらはすべて同じ ViewModel を共有しています。

検索フィルターをリクエストするときにすべてのフィールドにバインド プレフィックスを追加できますが、[Bind(Prefix = "")] をリクエスト パラメーターからの値で動作させる方法がわかりません。

// get the search filters with the bindingPrefix we need
public ActionResult Search(string bindingPrefix)
{
    ViewData.TemplateInfo.HtmlFieldPrefix = bindingPrefix;
    SearchViewModel model = new SearchViewModel
    {
        BindingPrefix = bindingPrefix
    };

    return PartialView("_SearchFilters", model); 
}

// post the search filters values
[HttpPost]
public ActionResult Search([Bind(Prefix = model.BindingPrefix)]SearchViewModel model)
{

}
4

1 に答える 1

6

なぜこれをやりたいのかわかりませんが、これはうまくいくはずです。

ビューのフォームには、非表示の値があります

@Html.Hidden("BindingPrefix", Model.BindingPrefix)

アクションを次のように変更します

[HttpPost]
public ActionResult Search(SearchViewModel model)
{
    UpdateModel(model, model.BindingPrefix);
}
于 2013-07-25T15:45:52.700 に答える