I have a type:
我有一个类型:
public class IssueForm
{
Order Order {get; set;}
Item Item {get; set;}
Range Range {get; set;}
}
I created a custom model binder due to requirements on Order and Item, but Range could still use the Default Model Binder.
由于订单和项目的需求,我创建了一个自定义模型绑定器,但是Range仍然可以使用默认的模型绑定器。
Is there a way from within my custom model binder to call the default model binder to return a Range object? I think I just have to just setup ModelBindingContext correctly, but I don't know how.
在我的自定义模型绑定器中是否有方法调用默认的模型绑定器来返回范围对象?我想我只需要正确地设置ModelBindingContext,但是我不知道怎么做。
EDIT
编辑
Looking at the first comment and answer -- it seems like inheriting from the default model binder could be useful.
查看第一个注释和答案——似乎从默认的模型绑定器继承是有用的。
To add more specifics for my setup so far I have:
为了给我的设置添加更多的细节,我有:
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
}
}
This might be a stupid way of doing it... this is my first model binder. Just pointing out my current implementation.
这可能是一个愚蠢的做法……这是我的第一个模型粘合剂。只是指出我当前的实现。
EDIT #2
编辑# 2
So the answers to override BindProperty will work if I can hook into like a "I'm all done binding" method and call the Factory method with the properties.
因此,如果我可以像“我已经完成绑定”方法那样使用属性调用Factory方法,那么重写BindProperty的答案就会起作用。
I guess I really should look at the DefaultModelBinder implementation and quit being stupid.
我想我真的应该看看DefaultModelBinder实现,不要再傻了。
3 个解决方案
#1
24
Try something like this:
试试这样:
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);
}
}
#2
50
override the BindProperty from the DefaultModelBinder:
重写来自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
}
}
#3
6
I think I would have registered two different custom model binders, one for Order and one for Item, and let the default model binder handle the Range and the IssueForm.
我想我应该注册了两个不同的自定义模型绑定器,一个用于订单,一个用于项目,然后让默认的模型绑定器处理范围和问题表单。
#1
24
Try something like this:
试试这样:
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);
}
}
#2
50
override the BindProperty from the DefaultModelBinder:
重写来自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
}
}
#3
6
I think I would have registered two different custom model binders, one for Order and one for Item, and let the default model binder handle the Range and the IssueForm.
我想我应该注册了两个不同的自定义模型绑定器,一个用于订单,一个用于项目,然后让默认的模型绑定器处理范围和问题表单。