开发者

ASP.NET MVC 2 validation of Date using DataAnnotations not working

开发者 https://www.devze.com 2023-01-31 07:23 出处:网络
I am using the following in my model for a date control [DataType(DataType.DateTime)] public DateTime txtDateAppCompletion { get; set; }

I am using the following in my model for a date control

   [DataType(DataType.DateTime)]
   public DateTime txtDateAppCompletion { get; set; }

I also tried DataType.Date as I only want a date input.

   [DataType(DataType.Date)]
   public DateTime txtDateAppCompletion { get; set; }

For some reason when I run the form and I type anything that is not a date it does not validate it, not on server or on client(client validation is enabled).

开发者_高级运维Any Idea why I am having this problems? How to solve it?


Ok, after some research I found this in a Microsoft book.

■ Caution Even though [DataType] looks like a validation attribute along with the others in Data Annotations, ASP.NET MVC does not treat it as one, so don’t expect [DataType(DataType.EmailAddress)] to validate for legal e-mail addresses! [DataType] is an anomaly; even though it inherits from System.ComponentModel.DataAnnotations.ValidationAttribute, its IsValid() method is hard-coded to return true regardless of the property’s value. Microsoft has explained that [DataType] is only meant to serve as a hint for formatting data in a scaffolded UI...

0

精彩评论

暂无评论...
验证码 换一张
取 消