localize data annotations default messages ([Required] [StringLength] etc.)

前端 未结 2 1059
旧时难觅i
旧时难觅i 2020-12-23 15:12

if I decorate the properties of my ViewModels with attributes like this:

public class Vm
{

[Required]
[StringLength(35)]
public string Name {get;set;}

}
         


        
2条回答
  •  青春惊慌失措
    2020-12-23 15:42

    There is a much better solution using asp.net MVC 3 these days incase someone is looking for a newer and far better approach.

    http://blog.gauffin.org/2011/09/easy-model-and-validation-localization-in-asp-net-mvc3/

    For example:

    public class UserViewModel
    {
        [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.LocalizedStrings))]
        [LocalizedDisplayName(ErrorMessageResourceName = "UserId", ErrorMessageResourceType = typeof(Resources.LocalizedStrings))]
        [LocalizedDescription(ErrorMessageResourceName = "UserIdDescription", ErrorMessageResourceType = typeof(Resources.LocalizedStrings))]
        public int Id { get; set; }
    }
    

    SO related question - Mvc 3.0 DataAnnotations Localization

提交回复
热议问题