通过资源文件DisplayAttribute本地化应用 [英] Apply localization through resource files to DisplayAttribute
问题描述
我已经开始在Visual Studio中默认MVC4项目,
I've started a default MVC4 project in Visual Studio,
某处在我的模型是这一段代码
Somewhere in my model is this piece of code
public class LoginModel
{
[Required]
[Display(Name ="Name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
我想将它更改为这样的事情,因为我想尝试本地化(另一个类在其中工作,而不是在这里)(字符串=本地化RESX文件)
I want to change it to something like this because i want to try localization (which works within another class, but not here) (strings=resx file for localization)
public class LoginModel
{
[Required]
[Display(Name =strings.UserName)]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = strings.Password)]
public string Password { get; set; }
[Display(Name = strings.RememberMe)]
public bool RememberMe { get; set; }
}
该错误是它必须是一个常量表达式,但是当我让这样,然后我得到的东西像'性能指标缺乏访问
The error is it must be a constant expression, but when I make it like that, then I get something like 'property index lacks accessor'
我缺少的是在这里?为什么不能我只是分配一个字符串值的混账东西?
在Java中,这一切都是那么容易得多。希望你能帮助我。
What am I missing here?? Why cant I just assign a string value to the darn thing? In Java this is all so much easier. Hope you can help me out.
推荐答案
您应该使用资源。
在属性,你可以只设置不变的值。
You should use Resources. In attributes you can set only immutable values.
试试这个。
[Display(Name = "Remember me?", ResourceType = typeof(YourResourcesType))]
public bool RememberMe { get; set; }
和期待这种
的 ASP.NET MVC 3本土化与displayAttribute和自定义资源提供商
这篇关于通过资源文件DisplayAttribute本地化应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!