开发者

DataAnnotations and multi lingual web applications

开发者 https://www.devze.com 2023-02-05 22:18 出处:网络
ASP.NET MVC has a nice feature called DataAnnotations that can simplify validation of user input. I couldn\'t find a way to work with the builtin data annotations so that the validation message will c

ASP.NET MVC has a nice feature called DataAnnotations that can simplify validation of user input. I couldn't find a way to work with the builtin data annotations so that the validation message will change if the user is running the spanish version of my application. Can someone put an example that takes multiple languag开发者_Go百科es into account?


It will get quite ugly fast.

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; }
}

I got a prettier solution in my blog: http://blog.gauffin.org/2010/11/simplified-localization-for-dataannotations/

0

精彩评论

暂无评论...
验证码 换一张
取 消