MVC 4 sets attribute values ​​from resources

I am trying to do the following:

[Display(Name = Resources.LcmsBs.Models.UserName)] [ToolTip(Resources.LcmsBs.ToolTips.UserName)] public string UserName { get; set; } 

I get the following compilation error:

The attribute argument must be a constant expression, a typeof expression, or an expression to create an attribute type array

What is the best practice to avoid hard-coded constants in code? Is it possible to set attribute values ​​from resources?

+4
source share
2 answers

You have a slightly different question, but the answer to the following can be applied to your question

DataAnnotations and Resources do not play well

Something like the following

 [Display(ResourceType = typeof(Resources.LcmsBs.Models), Name = "UserName")] 
+9
source

If you are using C # version 6.0 or higher, the best way might be:

 [Display(Name = nameof(Resources.LcmsBs.Models.UserName), ResourceType = typeof(Resources.LcmsBs.Models))] 
+3
source

Source: https://habr.com/ru/post/1445732/


All Articles