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

      

  • 相关阅读:
    Unity学习
    C#文件操作
    Unity3D XLua热更新流程
    Unity编辑器扩展
    Unity性能优化
    Unity热更新 xLua
    Unity热更新 AssetBundle
    Quickcocos从安装到打包
    EasyTouch5插件使用 EasyTouch手势检测功能
    PHP CURL HTTPS内存泄露问题
  • 原文地址:https://www.cnblogs.com/zitjubiz/p/6220867.html
Copyright © 2011-2022 走看看