0

In all my projects, I have jQuery date pickers that format the date dd-MMM-yyyy which both users worldwide and the DateTime.parse method understand perfectly - sadly this does not appear to be the case for data annotation validation! I have my data annotation as below:

    [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; }

And my form refuses to submit with the error as below:

Date field

Does anyone know how I can make it validate, accept and modelbind a date value in this format?

4

1 回答 1

0

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

于 2012-07-05T11:52:53.153 回答