ASP.NET MVC – 混合自定义和默认模型绑定

我有一个types:

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

由于Order和Item的要求,我创build了一个自定义模型绑定器,但Range仍然可以使用默认模型绑定器。

有没有一种方法从我的自定义模型联编程序中调用默认的模型联编程序来返回一个Range对象? 我想我只需要正确设置ModelBindingContext,但我不知道如何。


编辑

看第一个评论和答案 – 它似乎像inheritance默认的模型联编程序可能是有用的。

为了给我的设置添加更多细节,我已经:

 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 } } 

这可能是一个愚蠢的做法…这是我的第一个模型活页夹。 只是指出我目前的实施。


编辑#2

所以重写BindProperty的答案将工作,如果我可以钩入像“我全部完成绑定”的方法,并调用具有属性的工厂方法。

我想我真的应该看看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); } } 

重写DefaultModelBinder中的BindProperty:

 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 } } 

我想我会注册两个不同的自定义模型活页夹,一个用于订单,一个用于项目,并让默认模型活页夹处理范围和IssueForm。