问题
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).
回答1:
Change the registration of the provider to
Rebind<ModelValidatorProvider>().To<AttributeValidatorProvider>();
回答2:
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();
}
来源:https://stackoverflow.com/questions/8356811/how-to-stop-ninject-from-overriding-custom-dataannotationsmodelvalidatorprovider