Is it possible to use a variable for `[Display (Name =" Something ")]` data annotation in MVC3 (C #)

not sure why, but the data annotation in MVC3 insists on having constant values ​​that I just can't understand for things like error messages and display names. I like these annotations, they are so easy to use and so powerful, but what if you need to support multiple languages?

Imagine I have the following model:

public class Person { public string First_Name { get; set; } } 

If I don't change anything and don't use the CRUD views that MVC3 will build for me, I get a shortcut next to the text boxes that say "First_Name", so I add data annotation to change the display name like this:

 public class Person { [Display(Name="First name")] public string First_Name { get; set; } } 

Which works just fine. But I want to specify a different display name depending on the user's language, using the i function made earlier by GetDisplayName(string ToGet, string Language) , which simply returns the string that interests me, but if I change the data annotation to this:

 public class Person { [Display(Name=GetDisplayName("First_Name", "English"))] public string First_Name { get; set; } } 

Then I get a compiler error telling me that annotation requires constant values, WHY ????

Does anyone know a way to do what I'm trying to do? Thanks

UPDATE

Well, it seems that the best way to do this is with .resx resource files, according to several answers below, as well as in other posts. which works great for the most part.

Does anyone know how I can request a resource with a variable name? not in the context of data attributes this time, but only in controllers and views.

Mostly the moment I get resources using @Resources.SomeKey , but I would like to be able to use this inside the function in @Resources["SomeOtherKey"] , where SomeOtherKey is a dynamically generated string.

+4
source share
5 answers

If you have translated property names in resource files that are supported out of the box ... there is no need to expand the scope.

Just use:

 [Display(Name = "Resource_Key", ResourceType = typeof(DataFieldLabels))] public string myProperty { get; set; } 

Where "Resource_Key" is the key in your resource files ... and "DataFieldLabels" is the name of your base resource file.

EDIT:

To dynamically access resource files, you can:

 ResourceSet resset = ResourceManager.GetResourceSet(culture, true, false); var translated = resset.GetString("myToken") 
+7
source

DataAnnotations parameter values ​​require constants, i.e. actual lines.

+1
source

You can assign constants only.

You can make an extension and assign keyValues ​​there, and then the extension is retrieved from another location, such as a database or resource file or web service or something else.

+1
source

You might want to check out Hanselman's post on internationalization. Maybe it will help

0
source
 [Display(Name = "strResourceString", ResourceType = typeof(Resourcefile.strResourceString))] 
-1
source

All Articles