Situation: There is an application with a frontend on ASP MVC 4.5.

The data is chased through its own json api. The api methods are action controllers.

The model often chases not full, but only changed properties. When it arrives at the controller, it is validated, but it often fakes (since often only changes come, and not all of it).

There is a "correct" validation, but it happens on the next level following the controllers.

Question: how to disable MVC validation, which occurs before the controller is called? (I'm talking about the one that installs ModelState.IsValid)?

(I do not want to validate each entity 2 times, once the file on the controller and once in its own code further)

    1 answer 1

    To disable validation in MVC, try clearing the ModelValidatorProviders in Global.asax.cs:

     protected void Application_Start() { // Other startup code... ModelValidatorProviders.Providers.Clear(); } 

    By the way, if you look at the source code of MVC 3, we will see three default providers there:

     public static class ModelValidatorProviders { private static readonly ModelValidatorProviderCollection _providers = new ModelValidatorProviderCollection() { new DataAnnotationsModelValidatorProvider(), new DataErrorInfoModelValidatorProvider(), new ClientDataTypeModelValidatorProvider() }; public static ModelValidatorProviderCollection Providers { get { return _providers; } } } 

    Providers of rules (logic) for checking Models describe well the work of these ModelValidationProviders.

    • Wow! Thanks for the link, I'll try tomorrow and mark the answer if it works. - Oleg Nechitaylo
    • Strange but not working. I removed providers, added my own, but neither GetValidators of the provider, nor Validate ModelValidator are called, and standard validation continues to work. - Oleg Nechitaylo
    • What kind of standard validation continues? Maybe these are binding errors, for example, an incorrect date or number format? - Tolyandre
    • It is the errors associated with the attributes (Required, Regex, ...) that trigger, even the messages I get from them. My provider is not called, although I added it to the collection and for the whole life of the request it is there alone. - Oleg Nechitaylo