Using MVC 5 I need to localize an ErrorMessage for a DataAnnotation attributes. I receive the following error
ERROR
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
In model
[Compare("Password", ErrorMessage = Resources.Account_Register_ConfirmPasswordErrorMessage)]
public string ConfirmPassword { get; set; }
Any idea how to fix it?
You need to use
ErrorMessageResourceName
andErrorMessageResourceType
properties.For example like this:
Hope this helps!
Regards, Uros
You don't need anything, just create your resource file in right place.
For example Resources > ViewModels > LoginVm.ka-GE.resx (Georgian culture-info)
in LoginVm:
[Required(ErrorMessage = "UserName is Required")]
and in LoginVm.ka-GE.resx just add
UserName is Required
>სახელი არის აუცილებელი
< (it's Georgian Language)and all done.
Here's a detailed explanation on how to do this:
You add appropriate culture resx files to conventional folders, and tell the DA engine to look through them:
So for this model:
You'd add a resx file in one of the following locations:
For non-ASP.NET environments such as WPF, WinForms or others, see this answer.