How to stop Ninject from overriding custom DataAnnotationsModelValidatorProvider?

I have a custom DataAnnotationsModelValidatorProvider for more accurate model validation and then just adding attributes. I tried adding my resource to global.asax.cs as follows:

ModelValidatorProviders.Providers.Clear(); ModelValidatorProviders.Providers.Add(new AttributeValidatorProvider()); 

But as soon as I upload my form, I get an error: "The names of validation types in unobtrusive client validation rules must be unique. The next type of validation was viewed more than once: required."

According to a comment on this blog , this is due to the fact that Ninject redefines specialized vendors of validators.

I am new to MVC and I cannot find a way to tell Ninject to accept my custom providers as well, how would I decide to fix this problem?

For the record: I don't want to use Fluentvalidation.net, I want to stick to standard MVC confirmations (for the most part).

+6
source share
2 answers

Change provider registration to

 Rebind<ModelValidatorProvider>().To<AttributeValidatorProvider>(); 
+6
source

There is another way (works in MVC 4):

Find your class that inherits the IdependencyResolver interface and adds _kernel.Unbind<ModelValidatorProvider>(); to the constructor _kernel.Unbind<ModelValidatorProvider>(); - you just disable the ninject validator and there should be no collaboration with the default validator.

In my case, my constructor looks like this:

 public NinjectDependencyResolver() { _kernel = new StandardKernel(); _kernel.Unbind<ModelValidatorProvider>(); AddBindings(); } 
+4
source

Source: https://habr.com/ru/post/902929/


All Articles