How to stop Ninject from overriding custom DataAnnotationsModelValidatorProvider?

∥☆過路亽.° 提交于 2019-12-01 01:56:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!