这个是我们常见的AccountModel代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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 ; } } |
如果要支持多语言,要怎么做呢, 看这个例子
1 2 3 4 5 6 7 8 9 10 11 | 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
2014-12-25 数据分区考虑