数据注释错误消息
所有 DataAnnotation 验证属性都可以采用 ErrorMessageResourceType
和 ErrorMessageResourceName
的命名参数来指定验证失败时的错误消息:
[Display(Name = "Stock Date")]
[Required(ErrorMessageResourceType = typeof(ValidationMessages), ErrorMessageResourceName = "Required")]
public DateTime StockDate { get; set; }
使用这些命名参数而不是引用是否有任何优势直接获取资源?
[Display(Name = "Stock Date")]
[Required(ErrorMessage = ValidationMessages.Required)]
public DateTime StockDate { get; set; }
在这个项目之前我没有太多使用资源文件,所以可能有一些东西我遗漏了,但是直接引用资源似乎更干净,并且可以使用智能感知来捕获拼写错误。
All of the DataAnnotation validation attributes can take a named parameter of ErrorMessageResourceType
and ErrorMessageResourceName
to specify an error message when vaildation fails:
[Display(Name = "Stock Date")]
[Required(ErrorMessageResourceType = typeof(ValidationMessages), ErrorMessageResourceName = "Required")]
public DateTime StockDate { get; set; }
Is there any advantage to using these named parameters instead of referencing the resource directly?
[Display(Name = "Stock Date")]
[Required(ErrorMessage = ValidationMessages.Required)]
public DateTime StockDate { get; set; }
I haven't used resource files much before this project so there could be something I'm missing but referencing the resource directly seems a lot cleaner as well as catching typos with Intellisense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论