简体   繁体   English

控制器级别的模型绑定器

[英]Model binder at controller level

Is there a way to implement the following: 有没有一种方法可以实现以下目的:

// Model.cs

[ModelBinder(typeof(DefaultModelBinder))]
public class Model
{
}

public class DefaultModelBinder: IModelBinder
{
}

public class CustomModelBinder: DefaultModelBinder
{
}

// Controller1.cs

public class Controller1: Controller
{
    public virtual ActionResult Method(Model model)
    {
    }
}

// Controller2.cs

[ModelBinder(typeof(Model), typeof(CustomModelBinder))] // imaginary attribute
public class Controller2: Controller
{
    public virtual ActionResult Method(Model model)
    {
    }
}

I am aware of the ModelBinder at action level, however given a bunch of actions it does not follow DRY principle, as the whole controller is to use the CustomModelBinder . 我知道ModelBinder在操作级别,但是给了很多操作,它不遵循DRY原理,因为整个控制器都使用CustomModelBinder

Thanks. 谢谢。

Is this what you are looking for ? 这是你想要的 ?

using IModelBinder = System.Web.Mvc.IModelBinder;
using ModelBindingContext = System.Web.Mvc.ModelBindingContext;

public class CustomeModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        //your code here
    }
}

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

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