【问题标题】:Dataannotations localization数据注释本地化
【发布时间】:2013-05-30 13:41:05
【问题描述】:

型号

[MetadataType(typeof(UserMetaData))]
public class User
{
    public int Id { get; set; }
    public string UserName { get; set; }
}

public class UserMetaData
{
    public int Id { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources.ModelValidation), ErrorMessageResourceName = "UserNameRequired")]
    [LocalizedDisplayNameAttribute("UserName", NameResourceType = typeof(Resources.ModelValidation))]
    public string UserName { get; set; }
}

查看

@using (Html.BeginForm())
{
    <div>
        @Html.LabelFor(x => x.UserName)
        @Html.TextBoxFor(x => x.UserName)
        @Html.ValidationMessageFor(x => x.UserName)
    </div>
    <div>
        <input type="submit" value="Gönder" />
    </div>
}

LocalizedDisplayNameAttribute

public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
    private PropertyInfo _nameProperty;
    private Type _resourceType;

    public LocalizedDisplayNameAttribute(string displayNameKey)
        : base(displayNameKey)
    { }

    public Type NameResourceType
    {
        get { return _resourceType; }
        set
        {
            _resourceType = value;
            //initialize nameProperty when type property is provided by setter  
            _nameProperty = _resourceType.GetProperty(base.DisplayName, BindingFlags.Static | BindingFlags.Public);
        }
    }
    public override string DisplayName
    {
        get
        {              
            //check if nameProperty is null and return original display name value  
            if (_nameProperty == null) { return base.DisplayName; }
            return (string)_nameProperty.GetValue(_nameProperty.DeclaringType, null);
        }
    }
}

资源文件

输出

RequiredAttribute 本地化有效,但 LocalizedDisplayNameAttribute 无效。我找不到任何解决方案来解决这个问题。

有什么建议,缺的在哪里?

【问题讨论】:

  • 我没有这个!为什么要扩展 DysplayNameAttr?此控件支持作为 Requerid 的本地化!
  • @Fals,我不明白。你能解释一下吗? DisplayAttribute 不支持本地化
  • 是的,当您提供 ResourceType 属性时,Name 就是 resourceName!

标签: asp.net-mvc asp.net-mvc-4 localization data-annotations


【解决方案1】:

您似乎正在用这个LocalizedDisplayNameAttribute 属性重新发明轮子。此功能已内置在框架中,直接嵌入到您可以直接使用的 [Display] 属性中:

public class UserMetaData
{
    public int Id { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources.ModelValidation), ErrorMessageResourceName = nameof(Resources.ModelValidation.UserNameRequired)]
    [Display(ResourceType = typeof(Resources.ModelValidation), Name = nameof(Resources.ModelValidation.UserName))]
    public string UserName { get; set; }
}

至于为什么你的轮子改造不起作用,嗯,不能肯定地说。既然可以摆脱它,何必在意呢。

【讨论】:

  • 错误:Cannot retrieve property 'Name' because localization failed. Type 'Resources.ModelValidation' is not public or does not contain a public static string property with the name 'UserName'.
  • 您的资源被设置为内部,所以它失败了!检查打印屏幕!
  • @Fals,我知道这一点,但我无法改变它。有什么建议吗?
  • 我不知道这是什么Resources.ModelValidation。你应该把为你生成的类型放在那里。例如,如果您将 Resources.resx 文件放在 Web 应用程序的根目录中,并将 Custom Tool 设置为 PublicResXFileCodeGenerator 并在此资源文件中定义 UserName 键,那么您的属性应如下所示:[Display(Name = "UserName", ResourceType = typeof(Resources))] .这里重要的部分是将此资源文件的自定义工具设置为PublicResXFileCodeGenerator,以便设计器生成公共属性,而不是内部属性。
  • 好的,找到了。阅读我的这个答案:stackoverflow.com/a/14761762/29407 如果您想将资源文件保存在App_GlobalResources 中,您必须将其设置为嵌入式资源。是的,在这种情况下你应该使用typeof(Resources.ModelValidation)
猜你喜欢
  • 1970-01-01
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多