DataAnnotations和多语言Web应用程序

时间:2011-01-22 07:49:29

标签: validation asp.net-mvc-3

ASP.NET MVC有一个很好的功能,称为DataAnnotations,可以简化用户输入的验证。我找不到使用内置数据注释的方法,以便在用户运行我的应用程序的西班牙语版本时验证消息将更改。有人可以举一个考虑多种语言的例子吗?

1 个答案:

答案 0 :(得分:3)

快速变得非常丑陋。

public class User
{
    [Required(ErrorMessageResourceName = "Validation_Required", ErrorMessageResourceType = typeof(ModelTranslations))]
    public int Id { get; set; }

    [Required(ErrorMessageResourceName = "Validation_Required", ErrorMessageResourceType = typeof(ModelTranslations))]
    [StringLength(40, ErrorMessageResourceName = "Validation_StringLength", ErrorMessageResourceType = typeof(ModelTranslations))]
    public string FirstName { get; set; }

    [Required(ErrorMessageResourceName = "Validation_Required", ErrorMessageResourceType = typeof(ModelTranslations))]
    [StringLength(40, ErrorMessageResourceName = "Validation_StringLength", ErrorMessageResourceType = typeof(ModelTranslations))]
    public string LastName { get; set; }
}

我的博客中有一个更漂亮的解决方案:http://blog.gauffin.org/2010/11/simplified-localization-for-dataannotations/