【问题标题】:Apply localization through resource files to DisplayAttribute通过资源文件将本地化应用到 DisplayAttribute
【发布时间】:2012-06-09 06:07:03
【问题描述】:

我已经在 Visual Studio 中启动了一个默认的 MVC4 项目,

在我的模型中某处是这段代码

 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 文件)

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; }
  }

错误是它必须是一个常量表达式,但是当我这样做时,我会得到类似“属性索引缺少访问器”之类的东西

我在这里错过了什么?为什么我不能只为该死的东西分配一个字符串值? 在 Java 中,这一切都容易得多。希望你能帮帮我。

【问题讨论】:

  • 谢谢!!那个帖子解决了我的问题,我已经搜索过了,但我想我没有搜索正确。我不得不使用 [Display(ResourceType = typeof(strings), Name = "Gebruikersnaam")] 并且我将 strings.resx 属性全部公开

标签: c# asp.net-mvc variables static constants


【解决方案1】:

您应该使用资源。 在属性中,您只能设置不可变的值。

试试这个。

[Display(Name = "Remember me?", ResourceType = typeof(YourResourcesType))]
public bool RememberMe { get; set; }

看看这个 ASP.NET MVC 3 localization with DisplayAttribute and custom resource provider

【讨论】:

  • 我已经有了解决方案(见问题下的评论),但你是对的,这就是解决这个问题的方法。
【解决方案2】:

我认为你必须使用会话和视图状态,

使用会话访问http://msdn.microsoft.com/en-us/library/ms178581.aspx

并使用 viewstate 访问http://msdn.microsoft.com/en-us/library/ms972976.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    相关资源
    最近更新 更多