我有一个类型:
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
因此,如果我可以像“我已完成绑定”方法一样挂载并使用属性调用 Factory 方法,则覆盖 BindProperty 的答案将起作用。
我想我真的应该看看 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
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)