Asp.net mvc 3 使用ASP.NET MVC 3数据批注进行客户端和服务器端验证时,日期失败

Asp.net mvc 3 使用ASP.NET MVC 3数据批注进行客户端和服务器端验证时,日期失败,asp.net-mvc-3,data-annotations,unobtrusive-validation,Asp.net Mvc 3,Data Annotations,Unobtrusive Validation,在我所有的项目中,我都有jQuery日期选择器,用于格式化日期dd-MMM-yyyy 这是全球用户和DateTime.parse方法都能完全理解的-遗憾的是,数据注释验证似乎并非如此!我的数据注释如下: [Display(Name = "Date of Birth")] [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:d-MMM-yyyy}", ApplyFormatInEditMode =

在我所有的项目中,我都有jQuery日期选择器,用于格式化日期
dd-MMM-yyyy
这是全球用户和
DateTime.parse方法都能完全理解的-遗憾的是,数据注释验证似乎并非如此!我的数据注释如下:

    [Display(Name = "Date of Birth")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:d-MMM-yyyy}", ApplyFormatInEditMode = true)]
    [Required(ErrorMessage = "You must enter a date of birth.")]
    public DateTime dob { get; set; }
我的表格拒绝提交,错误如下:

    [Display(Name = "Date of Birth")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:d-MMM-yyyy}", ApplyFormatInEditMode = true)]
    [Required(ErrorMessage = "You must enter a date of birth.")]
    public DateTime dob { get; set; }


有人知道我如何用这种格式验证、接受和模型绑定日期值吗?

您可以像我一样为
DateTime
类型编写一个自定义模型绑定器,在从请求解析回DateTime字段时使用
[DisplayFormat]
属性中定义的格式。默认情况下,模型绑定器使用当前线程的CultureInfo设置或您在web.config的
元素中配置的值。如果将其设置为
auto
,则ASP.NET将使用客户端
Accept Language
请求头来调整区域性信息。

谢谢Darin,问题是包含这些日期字段的表单甚至不会提交,因为客户端验证也失败了。想法?谁在执行客户端验证?MS unobtrusive js库不会这样做。您似乎正在使用jQuery UI dateicker插件执行一些自定义验证。你能展示你的代码吗?