zoukankan      html  css  js  c++  java
  • DataAnnotations里DisplayAttribute和 ValidationAttribute 如何从资源文件读取信息,显示多语言

    这个是我们常见的AccountModel代码

       public class ChangePasswordModel
        {
            [Required]
            [DataType(DataType.Password)]
            [Display(Name = "Current password")]
            public string OldPassword { get; set; }
    
            [Required]
            [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
            [DataType(DataType.Password)]
            [Display(Name = "New password")]
            public string NewPassword { get; set; }
    
            [DataType(DataType.Password)]
            [Display(Name = "Confirm new password")]
            [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
            public string ConfirmPassword { get; set; }
        }
    
        public class LoginModel
        {
            [Required]
            [Display(Name = "User name")]
            public string UserName { get; set; }
    
            [Required]
            [DataType(DataType.Password)]
            [Display(Name = "Password")]
            public string Password { get; set; }
    
            [Display(Name = "Remember me?")]
            public bool RememberMe { get; set; }
        }
    

    如果要支持多语言,要怎么做呢, 看这个例子

        public class AddPointModel
        {
            [Required(ErrorMessageResourceType = typeof(ErrMsg),ErrorMessageResourceName="MobileRequire")]
            [DataType(DataType.PhoneNumber)]
            [Display(Name = "Mobile", ResourceType = typeof(Re))]
            public string Mobile { get; set; }
    
            [Required(ErrorMessageResourceType = typeof(ErrMsg), ErrorMessageResourceName = "CodeRequire")]
            [Display(Name = "积分码")]
            public string JfCode { get; set; }
        }
    

      Require 里面本身就支持从资源文件里读取语言信息。 只要在APP_GlobalResources里建Resource文件,指定文件类名,Key就可以了。

    但是Display这样写就不行。因为默认的Resource文件的Key是Internal的,访问不了。

    需要把Resource文件不要放在APP_GlobalResources里,改为嵌入的资源,自定义工具改成PublicResXFileCodeGenerator

    请参考这里 http://stackoverflow.com/questions/2431333/displayname-attribute-from-resources

      

  • 相关阅读:
    获取和设置iframe中的元素
    css隔行换色样式修改
    在本地打开网页
    HTML-embed标签详解
    GlusterFS缺点分析[转]
    设计新Xlator扩展GlusterFS[转]
    C语言:全局变量在多个c文件中公用的方法 [转]
    const char*, char const*, char*const的区别
    C 语言字符数组的定义与初始化
    滑动窗口机制[转]
  • 原文地址:https://www.cnblogs.com/zitjubiz/p/6220867.html
Copyright © 2011-2022 走看看