バインディングカスタムおよびデフォルトモデルをミキシング: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);
}
}
IModelBinderを実装するのではなく、最も洗練されたソリューションであるかどうかはわかりませんが、カスタムモデルバインダーをDefaultModelBinderから派生させることはできますか?次に、BindModelをオーバーライドするときに、必要に応じてDefaultModelBinderに責任を渡します。それ以外の場合は、カスタムバインドを使用します。 –
Yah、IModelBinderとDefaultModelBinderのいくつかのモデルバインダーが表示されています。 DefaultModelBinderには、BindModelメソッドから呼び出すと仮定したいくつかの余分なメソッドがあると思いますので、もっとニーズに集中できますか? – anonymous
ModelBinderとコードの入力処理と検証を手動で無視することを決定しました。今、私はそれを一貫性があり、透明で、現在の、そして想像を絶する将来のニーズに合わせて調整することができます。 – User