My ValidationAttributes don’t access my ErrorMessages value-pairs stored in a .resx Resource File generated with VS2010 built in resource generator. (Add New Item – Resources File)
I simply added a folder named Resources, then added a new Resources File and called it ErrorResources, in the resources editor I added a value pair StringLengthError – Too Long**.
Then in my DataAnnotation.ValidationAttribute I wrote:
[StringLength(5, MinimumLength = 3,
ErrorMessageResourceType = typeof(ErrorResources),
ErrorMessageResourceName = "StringLengthError")]
public string Details { get; set; }
I added a using statement to include the ErrorResources.
But when I run it, the ValidationAttribute StringLength is totally void.
If I use it without the ErrorResources, as in :
[StringLength(5, MinimumLength = 3)]
public string Details { get; set; }
The validation attribute comes back to life.
I tried accessing the ErrorResources value pairs in other part of the code, and it works. I just can’t get my ValidationAttributes to use the ErrorResources resource.
Access modifier should be
Publicbut that’s not enough.You will have to change properties of your resx file to look like this:
Pay attention to
Build ActionandCustom ToolIn addition
Resxused by data annotations should sit inApp_GlobalResourcesrather than inResourcesyou’ve mentioned. Try these, see if that works alright.Hope this will help you.