在asp.net mvc中覆盖属性的dataType不匹配消息(并通过资源文件显示消息)

时间:2012-02-07 08:25:25

标签: asp.net asp.net-mvc asp.net-mvc-3 data-annotations

想要覆盖datetime“错误的格式消息”。 即默认情况下DateTime属性显示消息:: The value '12--2002' is not valid for * dateTime PropertyName: (DD/MM/YYYY). 但我希望通过一些资源文件显示此消息。

 public class candidateAddressViewModel
 { [Required(ErrorMessageResourceType = typeof(myApp.Resources.res.Resource),
          ErrorMessageResourceName = "error_format")]
   [Display(Name = "lived_from", ResourceType = typeof(myApp.Resources.res.Resource)), DisplayFormat(DataFormatString = "{0:dd-MM-yy}", ApplyFormatInEditMode = true)]
    public DateTime? dateTimePropertyName{ get; set; }
 } 

建议我最简单的方式。

1 个答案:

答案 0 :(得分:0)

在App_Global_Resources中创建一个资源文件(例如“Messages.resx”。添加两个字符串:

PropertyValueInvalid({1}的值{0}无效。)

PropertyValueRequired。 (字段{0}是必需的。)

在Global.asax中添加:

DefaultModelBinder.ResourceClassKey = "Messages";