Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 为衍生对象及其基础添加自定义模型绑定器_Asp.net Mvc_Modelbinders - Fatal编程技术网

Asp.net mvc 为衍生对象及其基础添加自定义模型绑定器

Asp.net mvc 为衍生对象及其基础添加自定义模型绑定器,asp.net-mvc,modelbinders,Asp.net Mvc,Modelbinders,我的类图: BaseContentClass 页面继承BaseContentClass 选项卡继承BaseContentClass 如果我这样做 ModelBinders.Binders.Add(typeof(BaseContentObject), new BaseContentObjectCommonPropertiesBinder()); 然后,当出现“类型”选项卡的“控制器内操作”参数时,不会触发自定义模型绑定器 如果我这样做就会被解雇: ModelBinders.Binders.

我的类图:

BaseContentClass

  • 页面继承BaseContentClass
  • 选项卡继承BaseContentClass
如果我这样做

ModelBinders.Binders.Add(typeof(BaseContentObject), new BaseContentObjectCommonPropertiesBinder());
然后,当出现“类型”选项卡的“控制器内操作”参数时,不会触发自定义模型绑定器

如果我这样做就会被解雇:

ModelBinders.Binders.Add(typeof(Tab), new BaseContentObjectCommonPropertiesBinder());

但是我不想在global.asax.cs中编写“n”个Add语句来将所有派生类与我的自定义模型绑定器相关联,是吗?我不知道是否还有其他选择。

该类型使用该类型作为键。因此,它将忽略为基类注册模型绑定器。阅读本文,我认为您可以通过在类型上定义binder属性来解决这个问题,请参阅本文中的顺序优先级。

尝试这样做。我还没有测试过它,但我相当肯定它会起作用

[ModelBinder(typeof(BaseContentObjectCommonPropertiesBinder))]
public class BaseContentObject {}