如何将字段名称添加到本地化的必填字段验证器?

时间:2012-03-05 14:23:37

标签: asp.net asp.net-mvc-3 localization attributes

在字段上使用Required属性时,默认消息为“字段名称字段是必需的”。

我见过的大多数本地化示例都为每个字段提供了独特的资源:

  [Display(Name = "Field1", ResourceType = typeof(Resources.Test))]
  [Required(ErrorMessageResourceName = "Field1Required", ErrorMessageResourceType = typeof(Resources.Test))]
  public string field1 { get; set; }

  [Display(Name = "Field2", ResourceType = typeof(Resources.Test))]
  [Required(ErrorMessageResourceName = "Field2Required", ErrorMessageResourceType = typeof(Resources.Test))]
  public string field2 { get; set; }

是否有任何方法可以使用本地化资源复制默认功能,以便我可以使用以下内容:

  [Display(Name = "Field1", ResourceType = typeof(Resources.Test))]
  [Required(ErrorMessageResourceName = "resRequired", ErrorMessageResourceType = typeof(Resources.Test))]
  public string field1 { get; set; }

  [Display(Name = "Field2", ResourceType = typeof(Resources.Test))]
  [Required(ErrorMessageResourceName = "resRequired", ErrorMessageResourceType = typeof(Resources.Test))]
  public string field2 { get; set; }

其中resRequired类似于:The {0} field is required.

1 个答案:

答案 0 :(得分:1)

嗯,事实证明只需将我的资源值更改为“{0}字段是必需的”就可以了。