ASP.NET MVC 2中的DataAnnotations - 阻止MVC将RequiredAttribute应用于不可为空的DateTime等属性

时间:2022-07-13 01:49:20

Im trying to create a custom version of the RequiredAttribute to replace the built in one and I've got it working for properties that have strings values, but with properties that are DateTime or integer for example, the default RequiredAttribute seems to be applied automatically (IF the property is not nullable!)

我试图创建一个RequiredAttribute的自定义版本来替换内置的版本,我已经让它适用于具有字符串值的属性,但是具有DateTime或整数的属性,默认的RequiredAttribute似乎是自动应用的(如果财产不可空!)

My problem is that i want to be able to specify a DateTime property as required using my custom required validator which gets the error message from a resources file (I don't want to have to tell the RequiredAttribute the type of the resource file and the key every time i apply it. That is why I'm making a custom one.)

我的问题是我希望能够使用我的自定义必需验证器指定一个DateTime属性,该验证器从资源文件中获取错误消息(我不想告诉RequiredAttribute资源文件的类型和每次我申请它都是关键。这就是为什么我要定制一个。)

How can i prevent the framework from applying the required attribute to properties of type DateTime and int etc without changing them to nullable.

如何防止框架将所需属性应用于DateTime和int等类型的属性,而不将它们更改为可为空。

Thanks

3 个解决方案

#1


10  

Found it! I put this in the Global.asax.cs file

找到了!我把它放在Global.asax.cs文件中

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

The DataAnnotationsModelValidatorProvider class has a static property called "AddImplicitRequiredAttributeForValueTypes" which by default must be true, and setting it to false fixed the problem.

DataAnnotationsModelValidatorProvider类有一个名为“AddImplicitRequiredAttributeForValueTypes”的静态属性,默认情况下必须为true,并将其设置为false可修复问题。

(For anyone trying to do the same sort of things that finds this entry, I'm documenting it here)

(对于任何试图做同样事情的人来说,我都在这里记录)

#2


1  

I have RTM installed and dont have the AddImplicitRequiredAttributeForValueTypes property...

我安装了RTM,没有AddImplicitRequiredAttributeForValueTypes属性......

#3


1  

see post http://thelongwayback.wordpress.com/2010/02/25/mvc2-rc-different-assemblies-addimplicitrequiredattributeforvaluetypes/

见http://thelongwayback.wordpress.com/2010/02/25/mvc2-rc-different-assemblies-addimplicitrequiredattributeforvaluetypes/

#1


10  

Found it! I put this in the Global.asax.cs file

找到了!我把它放在Global.asax.cs文件中

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

The DataAnnotationsModelValidatorProvider class has a static property called "AddImplicitRequiredAttributeForValueTypes" which by default must be true, and setting it to false fixed the problem.

DataAnnotationsModelValidatorProvider类有一个名为“AddImplicitRequiredAttributeForValueTypes”的静态属性,默认情况下必须为true,并将其设置为false可修复问题。

(For anyone trying to do the same sort of things that finds this entry, I'm documenting it here)

(对于任何试图做同样事情的人来说,我都在这里记录)

#2


1  

I have RTM installed and dont have the AddImplicitRequiredAttributeForValueTypes property...

我安装了RTM,没有AddImplicitRequiredAttributeForValueTypes属性......

#3


1  

see post http://thelongwayback.wordpress.com/2010/02/25/mvc2-rc-different-assemblies-addimplicitrequiredattributeforvaluetypes/

见http://thelongwayback.wordpress.com/2010/02/25/mvc2-rc-different-assemblies-addimplicitrequiredattributeforvaluetypes/