How to localize Range attribute?

北城余情 提交于 2021-02-07 19:43:39

问题


So I need to localize this

 [Range(1, 150, ErrorMessage = "")]

I tried to use

[Range(1, 150, ErrorMessage = MyApp.Properties.Resource.ErrorMessageMustBeBetween)]

where MyApp.Properties.Resource.ErrorMessageMustBeBetween is

"{0} must be between {1} and {2}."

and it says

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

How it could be done then? Thank you!


回答1:


You need to use ErrorMessageResourceType and ErrorMessageResourceName to use a resource for a Data Annotation.

Try:

[Range(1, 150, ErrorMessageResourceType = typeof(MyApp.Properties.Resource), ErrorMessageResourceName = "ErrorMessageMustBeBetween")]


来源:https://stackoverflow.com/questions/25160778/how-to-localize-range-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!