2012-04-16 13 views
0

ASP.NET MVC(4)アプリケーションでは、名前が不十分な(IMO)クエリ文字列パラメータを自動的に返すサードパーティのJavascriptライブラリを使用しています。BindAttributeをグローバルに使用する

public ActionResult MyAction([Bind(Prefix="rp")] int pageSize = 50) 
{ 
} 

しかし、私はページングを使用するすべての場所内のこのコードはかなり迅速に退屈ます:私は、次のように結合モデルで、この値を傍受することができます。

特定のプレフィックス/置換の組み合わせで[BindAttribute]をグローバルに設定することはできますか?

回避策は、Javascriptライブラリを変更することです(望ましくありません)。または手動でRequest.QueryStringプロパティからパラメータを取得します。私は物事をきれいに保つことを望んでいた。お使いのコントローラのアクション(複数可)に

public class MyViewModel 
{ 
    public int PageSize { get; set; } 
} 

答えて

2

あなたは、ビューモデルを使用することができ

public ActionResult MyAction(MyViewModel model) 
{ 
    ... 
} 

し、このビューモデル用のカスタムモデルバインダー書く:

public class MyViewModelBinder: DefaultModelBinder 
{ 
    protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) 
    { 
     bindingContext.ModelName = "rp"; 
     base.BindProperty(controllerContext, bindingContext, propertyDescriptor); 
    } 
} 
Application_Startに登録される

ModelBinders.Binders.Add(typeof(MyViewModel), new MyViewModelBinder()); 
関連する問題