Providing localized error messages for non-attributed model validation in ASP.Net MVC 2?

Posted by Lance McNearney on Stack Overflow See other posts from Stack Overflow or by Lance McNearney
Published on 2010-03-19T21:01:01Z Indexed on 2010/03/31 8:43 UTC
Read the original article Hit count: 333

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?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about localization