2009-06-09 10 views
36

バインディングカスタムおよびデフォルトモデルをミキシング:ASP.NET MVC - 私はタイプ持っ

public class IssueForm 
{ 
    Order Order {get; set;} 
    Item Item {get; set;} 
    Range Range {get; set;} 
} 

私が注文して項目に要件によるカスタムモデルバインダーを作成しますが、範囲はまだ初期のモデルバインダーを使用することができます。

私のカスタムモデルバインダーの中から、デフォルトのモデルバインダーを呼び出してRangeオブジェクトを返す方法はありますか?私はちょうどちょうどModelBindingContextを正しくセットアップしなければならないと思うが、私は方法を知らない。最初のコメントとの回答を見てみるとEDIT


- それは役に立つかもしれないデフォルトのモデルバインダーから継承するように思えます。

これまでのところ、私は持っている私のセットアップのためのより多くの詳細を追加するには:

public IssueFormModelBinder : IModelBinder 
{ 
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     Order = //code to pull the OrderNumber from the context and create an Order 
     Item = //code to pull the ItemNumber from the context and create an Item 

     IssueForm form = IssueFormFactory.Create(Order, Item); 

     form.Range = // ** I'd like to replace my code with a call to the default binder ** 

     return form 
    } 
} 

これはそれを行うことの愚かな方法かもしれない...これが私の最初のモデルバインダーです。私の現在の実装を指摘してください。


EDIT#2

だから、答えは私がメソッド「私はすべてのバインディングを終わりだ」とプロパティでファクトリメソッドを呼び出すようにフックすることができた場合は、bindPropertyが動作する上書きします。

私は本当にDefaultModelBinderの実装を見て、愚かではないと思います。 DefaultModelBinderから

public class CustomModelBinder : DefaultModelBinder { 
    protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) { 
     if(propertyDescriptor.Name == "Order") { 
      ... 
      return; 
     } 

     if(propertyDescriptor.Name == "Item") { 
      ... 
      return; 
     } 

     base.BindProperty(controllerContext, bindingContext, propertyDescriptor); 
    } 

} 
+1

IModelBinderを実装するのではなく、最も洗練されたソリューションであるかどうかはわかりませんが、カスタムモデルバインダーをDefaultModelBinderから派生させることはできますか?次に、BindModelをオーバーライドするときに、必要に応じてDefaultModelBinderに責任を渡します。それ以外の場合は、カスタムバインドを使用します。 –

+0

Yah、IModelBinderとDefaultModelBinderのいくつかのモデルバインダーが表示されています。 DefaultModelBinderには、BindModelメソッドから呼び出すと仮定したいくつかの余分なメソッドがあると思いますので、もっとニーズに集中できますか? – anonymous

+0

ModelBinderとコードの入力処理と検証を手動で無視することを決定しました。今、私はそれを一貫性があり、透明で、現在の、そして想像を絶する将来のニーズに合わせて調整することができます。 – User

答えて

24

はこのような何かを試してみてください

public class CustomModelBinder:DefaultModelBinder 
     { 
      protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) 
      { 
       if (propertyDescriptor.PropertyType == typeof(Range)) 
       { 
        base.BindProperty(controllerContext, bindingContext, propertyDescriptor); 
       } 
       // bind the other properties here 
      } 
     } 
+0

私はおそらくここでばかげているでしょうが、CustomModelBinderクラスをどのように実装していますか?確かに、これが動作するためにインスタンス化される必要がありますか?おそらくActionResultパラメータの属性としてですか? –

49

オーバーライドは、bindProperty:

+20

+1の代わりにinspedctingプロパティの型に+1します。マジックストリングを責める! –

6

を私は私は2つの異なるカスタムモデルバインダー、注文用と項目のための1つを登録していると思い、デフォルトのモデルバインダーにRangeとIssueFormを処理させます。

+4

はい、明らかに私はこの質問をして以来、しばらくしています。私はDefaultModelBinderコードを見て、モデルバインドの再帰的性質を実現しました。私は他の複雑な型を含む複雑な型を理解していませんでした。それぞれの型のバインダを個別に定義するだけでした。私は代わりに、複雑な親がそれぞれの複雑な子供の詳細を知る必要があると考えました。 – anonymous

関連する問題