ASP.NET MVC ModelBinding Inherited Classes

Luk*_*uke 2 asp.net-mvc inheritance modelbinders

我有一个关于ASP.NET MVC中的ModelBinding的问题(我正在使用MVC 2预览2)与继承相关.

假设我有以下接口/类:

interface IBase
class Base : IBase
interface IChild
class Child: Base, IChild
Run Code Online (Sandbox Code Playgroud)

我有一个自定义模型绑定器BaseModelBinder.

以下工作正常:

ModelBinders.Binders[typeof(Child)] = new BaseModelBinder();
ModelBinders.Binders[typeof(IChild)] = new BaseModelBinder();
Run Code Online (Sandbox Code Playgroud)

以下不起作用(绑定类型为Child的on对象):

ModelBinders.Binders[typeof(Base)] = new BaseModelBinder();
ModelBinders.Binders[typeof(IBase)] = new BaseModelBinder();
Run Code Online (Sandbox Code Playgroud)

有没有办法让基类的模型绑定器适用于所有继承的类?我真的不想为每个可能的继承类手动输入内容.

此外,如果可能,是否有办法覆盖特定继承类的模型绑定器?说我有这个工作,但我需要一个特定的Child2模型绑定器.

提前致谢.

que*_*en3 6

我采用了一个简单的路由,我只是在启动时使用反射动态注册所有派生类.也许它不是一个干净的解决方案,但它在初始化代码中的几行才有效;-)

但是,如果你真的想与模型粘合剂混乱(你会HAVE TO -终于-但有是更好的方式来度过你的时间;-)你可以看到这个这个.