Disable Model Validation in Asp.Net MVC

前端 未结 7 952
没有蜡笔的小新
没有蜡笔的小新 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:25

    I know that this already been answered but what you really needed was to extend the DataAnnotationsValidatorProvider and override the GetValidators method.

    Then, on startup, you would remove the DataAnnotationsValidatorProvider from ModelValidatorProviders.Providers and add your new one.

    Needless to say, if you simply don't want any validation at all, you can simply have the GetValidators method returning an empty collection.

    In my case, I need to remove validation only when submitting the forms while still keeping the client-side validation, hence the following:

    public class DynamicModelValidatorProvider : DataAnnotationsModelValidatorProvider
    {
       GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
        {
            if (context.HttpContext.Request.HttpMethod == "POST")
            {
                return new ModelValidator[] { };
            }
    
            return base.GetValidators(metadata, context, attributes);
        }
    }
    
    0 讨论(0)
  • 2020-12-09 21:27

    Just remove the items you don´t need before checking if the model is valid

    ModelState.Remove("Email");
    if (ModelState.IsValid)
    {
       // your logic
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-09 21:28

    I use [ ValidateInput( false )]

    Not sure if this prevents model validation, or only IIS submit validation.

    0 讨论(0)
  • 2020-12-09 21:29

    I would recommend you perform validation in both places rather than trying to turn off validation in the UI. I understand your point that the service cannot assume that it's being passed valid data - that is a valid concern and is the reason your service should have validation. But you should also have validation in your UI. This is also nice because you can have client-side validation to prevent user errors and give you users a better experience.

    0 讨论(0)
  • 2020-12-09 21:31

    I've solved this problem with this code:

    public ActionResult Totals(MyModel model)
    {
        ModelState.Clear();
        return View(model);
    }
    

    Not sure it's the right way though.

    0 讨论(0)
提交回复
热议问题