Providing localized error messages for non-attributed model validation in ASP.Net MVC 2?
- by Lance McNearney
I'm using the DataAnnotations attributes along with ASP.Net MVC 2 to provide model validation for my ViewModels:
public class ExamplePersonViewModel {
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.Validation))]
[StringLength(128, ErrorMessageResourceName = "StringLength", ErrorMessageResourceType = typeof(Resources.Validation))]
[DataType(DataType.Text)]
public string Name { get; set; }
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.Validation))]
[DataType(DataType.Text)]
public int Age { get; set; }
}
This seems to work as expected (although it's very verbose). The problem I have is that there are behind-the-scenes model validations being performed that are not tied to any specific attribute. An example of this in the above model is that the Age property needs to be an int. If you try to enter a non-integer value on the form, it will error with the following (non-localized) message:
The field Age must be a number.
How can these non-attribute validation messages be localized?
Is there a full list of these messages available so I can make sure they are all localized?