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.