Disable Model Validation in Asp.Net MVC

前端 未结 7 959
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 20:41

How do I disable Model validation for a single Action in a Controller ? Or can I do it per model by registering the model type at startup somewhere ?

I want the Mode

7条回答
  •  天命终不由人
    2020-12-09 21:28

    Unfortunately there seems to be no easy way to disable the model validation happening in the ModelBinder except for registering every single model type you don’t want to validate (including nested complex types) with a specific ModelBinder. It can be done with the following code:

    ModelBinders.Binders[typeof(MyModelType)] = new NonValidatingModelBinder();

    Creating a SkipValidationAttribute that can be attached to action methods or action method parameters doesn’t seem possible as it should be implemented in the ControllerActionInvoker, but there is no way of letting the ModelBinder know that it should do any validation in the SetProperty() and OnModelUpdated methods when calling BindModel() in the GetParameterValue() method.

提交回复
热议问题