问题描述
我想使用数据注释验证日期时间字段,但我遇到了问题。据对MSDN文档(http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.rangeattribute.aspx),下面应该做的工作。
I want to use Data Annotations to validate DateTime fields, but I'm running into problems. According to documentation on MSDN (http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.rangeattribute.aspx), the following should do the job
[Range(typeof(DateTime), "1/2/2004", "3/4/2004",
ErrorMessage = "Value for {0} must be between {1} and {2}")]
然而,这标志着任何日期我输入无效!
However, this marks any date I enter as invalid!
起初我还以为是不是捡英国时间(当我试图26/2/2004),但我甚至无法得到它的使用日期,如2004年2月2日。
At first I thought it was not picking up UK dates (when I tried 26/2/2004) but I can't even get it to use dates such as 2/2/2004.
我使用内MVC2的dataannotations,并使用客户方验证MicrosoftAjax框架。
I'm using the dataannotations within MVC2, and using the MicrosoftAjax framework for clientside validation.
有什么建议?
感谢
推荐答案
好了,几年已过了,我重新审视这个同样的问题与MVC4,我可以告诉你,它显然已经解决了。
Well, a few years have gone past and I revisited this same issue with MVC4 and I can tell you that it has apparently been resolved.
我创建了一个非常简单的默认MVC4网站,并给了一个日期成员如下属性
I created a very simple default MVC4 site, and gave a date member the following attributes
[Required]
[DataType(DataType.Date)]
[Range(typeof(DateTime), "1/2/2004", "3/4/2004", ErrorMessage = "Value for {0} must be between {1} and {2}")]
public DateTime BlogDate { get; set; }
验证现在工作完全根据英国数据系统,不允许日期2004年2月1日,允许2004年4月3日或26/3/2004日期。
The validation now works perfectly under UK data system, disallowing a date 2/1/2004, allowing a date of 4/3/2004 or 26/3/2004.
我使用该模板趁着code-第一EF4的,但我没有任何理由怀疑它没有被一般是固定的,因为JavaScript的正常工作了。
The template I was using took advantage of code-first EF4, but I don't have any reason to suspect that it hasn't been fixed generally, since the javascript is working properly too.
所以,如果你正在使用MVC2这可能仍然是一个问题,但我已经找到了最好的解决方案是,只要它是提供给你使用MVC4。
So if you are using MVC2 this may still be a problem, but the best solution which I've found is to use MVC4 as long as it is available to you.
这篇关于英国日期时间字段数据注释范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!