是否可以在MVC3(C#)中为变量[[Display(Name =" Something")]]数据注释使用变量 [英] Is it possible to use a variable for the `[Display(Name="Something")]` data annotation in MVC3 (C#)

查看:100
本文介绍了是否可以在MVC3(C#)中为变量[[Display(Name =" Something")]]数据注释使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道为什么,但是MVC3中的数据注释坚持要具有常量值,对于错误消息和显示名称之类的东西我根本无法理解.我喜欢这些注释,它们非常易于使用且功能强大,但是如果您需要支持多种语言呢?

not sure why, but the data annotation in MVC3 insist on having constant values, which i just can't understand for things like error messages and display names. I love 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; }
}

如果我不进行任何更改并使用MVC3会为我构建的CRUD视图,则会在文本字段旁边显示"First_Name"标签,因此我添加了数据注释来更改显示名称,如下所示:

If i dont change anything and use the CRUD views that MVC3 will build for me I get a label next to the text fields that says "First_Name", so i add the data annotation to change the display name like so:

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

哪个工作正常.但是我想根据用户的语言提供不同的显示名称,使用我先前制作的GetDisplayName(string ToGet, string Language)函数,该函数只返回我感兴趣的字符串,但是如果我将数据注释更改为:

Which works just fine. But i want to supply a different display name depending on the users language, using a function i made previously GetDisplayName(string ToGet, string Language) which just returns the string i am interested in, but if i change my 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 the annotation requires a constant values, WHY????

有人知道一种方法来完成我想做的事情吗?谢谢

Does anyone know a way to accomplish what i am trying to do? Thanks

更新

好吧,看来最好的方法是使用.resx资源文件,如以下几个答案以及其他帖子中的那样.在大多数情况下效果很好.

Ok, it appears that the best way to do this is with .resx resource files as per several answers below and those 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 just in controllers and views.

目前基本上我正在使用@Resources.SomeKey获取资源,但是我希望能够在@Resources["SomeOtherKey"]中的函数中使用该资源,其中SomeOtherKey是动态生成的字符串.

Basically at the moment i am getting at the resources with @Resources.SomeKey but i would like to be able to use that within a function in a @Resources["SomeOtherKey"] where SomeOtherKey is a dynamically generated string.

推荐答案

如果您在资源文件中拥有翻译后的属性名称,那么即开即用的支持...无需扩展框架.

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

只需使用:

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

"Resource_Key"是资源文件中的键...而"DataFieldLabels"是基础资源文件的名称.

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

为了动态访问资源文件,您可以执行以下操作:

In order to access resource files dynamically you can do:

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

这篇关于是否可以在MVC3(C#)中为变量[[Display(Name =" Something")]]数据注释使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆