Question

I have a custom DataAnnotationsModelValidatorProvider for doing model validation in a more dynamic way then just adding attributes. I tried to add my provide to the global.asax.cs like so:

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

But once I load my form, I get an error saying "Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: required".

According to a comment on this blog, this is because Ninject is overriding custom validator providers.

I'm fairly new to MVC and I can't seem to find a way to tell Ninject to accept my custom providers as well, how would I go about fixing this problem?

For the record: I do not wish to use Fluentvalidation.net, I want to stick with the default MVC validations (for the most part).

Was it helpful?

Solution

Change the registration of the provider to

Rebind<ModelValidatorProvider>().To<AttributeValidatorProvider>();

OTHER TIPS

There is another way (works in MVC 4 for sure):

Find your class which inherit IdependencyResolver interface and add to constructor _kernel.Unbind<ModelValidatorProvider>(); - you just unbind ninject validator and there should be no colission with default validator.

In my case my constructor looks like this:

public NinjectDependencyResolver()
{
       _kernel = new StandardKernel();
       _kernel.Unbind<ModelValidatorProvider>();
       AddBindings();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top