ASP.NET 5:DisplayAttribute的本地化是否已经消失?

时间:2015-10-20 16:28:32

标签: localization data-annotations asp.net-core

我正在将应用程序从以前的ASP.NET版本迁移到ASP.NET 5(vNext,MVC 6)。以前,我将DisplayAttribute的本地化表单附加到ViewModel的属性:

[Required(ErrorMessageResourceName = "FieldIsRequired", ErrorMessageResourceType = typeof(Resources.Validation))]
[Display(Name = "UserName", ResourceType = typeof(Resources.Common))]
public string UserName { get; set; }

我添加了DataAnnotations服务:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
            .AddViewLocalization(options => options.ResourcesPath = "Resources/Views")
            .AddDataAnnotationsLocalization();
}

当我提交无效表单时,错误消息会被本地化(在[Required]属性中指定)。

但是尝试显示表单时,我得到了一个异常(资源类中没有公共属性“UserName”),直到我注释掉[Display]属性。

似乎输入标签无法再使用[DisplayAttribute]进行本地化?

谢谢!

1 个答案:

答案 0 :(得分:1)

它确实消失了。根据{{​​3}}:

  

运行时不会查找非验证属性的本地化字符串。在上面的代码中,“电子邮件”(来自[显示(姓名="电子邮件")]将不会被本地化。

更新20.03.2017

根据documentation

,使用新的.NET Core SDK重新启用了非验证属性的本地化
  

DataAnnotations错误消息已使用IStringLocalizer<T>进行本地化。   使用选项ResourcesPath = "Resources"RegisterViewModel中的错误消息可以存储在以下任一路径中:

     
      
  • 资源/ ViewModels.Account.RegisterViewModel.fr.resx
  •   
  • 资源/的ViewModels /帐户/ RegisterViewModel.fr.resx
  •