简体   繁体   English

asp.net mvc是否可以使用两个自定义模型活页夹?

[英]asp.net mvc is it possible to use two custom model binders?

So I have one custom model binder that inherits from DefaultModelBinder, where I am overriding the BindProperty() method to handle a type of field we've created. 因此,我有一个自定义模型绑定程序,它继承自DefaultModelBinder,在这里我将重写BindProperty()方法以处理我们创建的字段类型。

I also have one controller that we'd like to override BindModel() on, since we're handling an object in session for multiple views with that controller. 我还有一个控制器想要覆盖BindModel(),因为我们正在使用该控制器在会话中处理多个视图的对象。

So I have CustomModelBinder : DefaultModelBinder, and then in the class where we override BindModel() i have that inheriting from CustomModelBinder. 所以我有CustomModelBinder:DefaultModelBinder,然后在我们重写BindModel()的类中,我有从CustomModelBinder继承的那个类。 SpecialModelBinder: CustomModelBinder SpecialModelBinder:CustomModelBinder

But I have set a breakpoint in our override of BindProperty() in CustomModelBinder, and this never gets hit when using the controller that is also overriding BindModel(). 但是我已经在CustomModelBinder中对BindProperty()的覆盖中设置了一个断点,当使用也覆盖BindModel()的控制器时,这个断点永远不会被击中。

Can I not inherit like this? 我不能这样继承吗? What's happening here? 这里发生了什么事?

Thank You! 谢谢!

edit: 编辑:

in global.asax: 在global.asax中:

ModelBinders.Binders.Add(typeof(ClassA), new SpecialModelBinder());
ModelBinders.Binders.Add(typeof(ClassB), new CustomModelBinder());
ModelBinders.Binders.Add(typeof(ClassC), new CustomModelBinder());
ModelBinders.Binders.Add(typeof(ClassD), new CustomModelBinder());

public class CustomModelBinder : DefaultModelBinder
{
    // this will be hit in controllers that handle classes B, C, and D, but will not be hit in controller that handles ClassA
    protected override void BindProperty(...){}
}

public class SpecialModelBinder : CustomModelBinder
{  
    // this will be hit when working in controller that handles ClassA only
    public override object BindModel(...){}
}

I recreated the scenario you described in an empty MVC application, and wasn't able to replicate the situation you describe here. 我重新创建了您在一个空的MVC应用程序中描述的方案,但无法复制此处描述的情况。 I had no problem subclassing SpecialModelBinder from CustomModelBinder and hitting both breakpoints in all classes. 我没有问题, SpecialModelBinderCustomModelBinder并在所有类中都达到两个断点。 Is there any other informaiton you can provide that might illuminate a solution? 您是否可以提供其他可能启发解决方案的信息? As you describe it here, there should be no issue with hitting both breakpoints for ClassA and only the BindProperty method for B, C and D. 正如您在此处描述的那样,击中ClassA两个断点以及击中B,C和D的BindProperty方法都应该没有问题。

Scenarios I tried: 1) Edit ClassA . 我尝试的场景:1)编辑ClassA Result: BindModel() on SpecialModelBinder called and BindProperty() on CustomModelBinder called for each property on the class; 结果: BindModel()SpecialModelBinder称为和BindProperty()CustomModelBinder要求对类的每个属性; 2) Edit ClassB (or C or D). 2)编辑ClassB (或C或D)。 Result: BindProperty() on CustomModelBinder called for each property on the class. 结果: BindProperty()CustomModelBinder要求对每类财产。

Is there anything else in your Application_Start() method in Global.asax , other than the Area and Route registration calls? 除了Area和Route注册调用之外, Global.asax Application_Start()方法中还有其他什么吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM