0

I am trying to localize validation messages in an ASP MVC3 model class.

[RegularExpression(@"^\d*$", ErrorMessage = "Has to be numeric.")]
public Int32? X{ get; set; }

We have to retrieve the localized strings from a database using a method from a given resource handler, so no common resource files are possible.

What is the prefered way to do this?

Thanks in advance!

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Michael
  • 23
  • 2

1 Answers1

0

You can set the resource key on the vaildation attribute, along with the resource type (RESX).

Required(ErrorMessageResourceName = "Required", 
           ErrorMessageResourceType = typeof(Resources.Validation))]
[DataType(DataType.Text)]
public string Name { get; set; }

Note one limitation is if you are using a custom resource provider. Unfortunately there's is no provision to plug this in. I came up with solution for this here, but if you are using standard RESX approach then it's not necessary.

Community
  • 1
  • 1
TheCodeKing
  • 19,064
  • 3
  • 47
  • 70