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

孤街醉人 提交于 2019-12-03 16:42:09

If you have your translated property names in Resource Files, that is supported out of the box... no need to extend the framework.

Just use:

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

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

EDIT:

In order to access resource files dynamically you can do:

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

DataAnnotations param values require constants, i.e. actual strings.

You can only assign constants.

You can do an extension and assign keyValues there and then the extension fetches from somewhere else, like a database or a resource file or a webservice or whatever.

You may want to check Hanselman's post regarding internationalization. Maybe it could help

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