ASP.NET MVC DataAnnotations doesn't accept dd-MM-yyyy format

拟墨画扇 提交于 2019-12-20 06:42:18

问题


In my ASP.NET MVC 4 project I have a DateOfBirth field that uses the DataAnnotations Validation Date Attribute...

[Required(ErrorMessage = "This field is required"), Date]
public DateTime DateOfBirth { get; set; }

I have set the format of my DatePicker control to dd-MM-yyyy (default was yyyy-MM-dd). But when I submit the form I am told that the date format is not valid.

dd-MM-yyyy is a valid date format so why would it complain? Is there any way to fix this?

Thanks


回答1:


try adding:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]


来源:https://stackoverflow.com/questions/16583790/asp-net-mvc-dataannotations-doesnt-accept-dd-mm-yyyy-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!