这个是我们常见的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