英国Datetime字段的数据注释范围 [英] Data Annotation Ranges of UK Datetime fields

查看:125
本文介绍了英国Datetime字段的数据注释范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用数据注释来验证DateTime字段,但是我遇到问题。根据MSDN上的文档( http:// msdn。 microsoft.com/en-us/library/system.componentmodel.dataannotations.rangeattribute.aspx ),以下工作应该执行

  [Range(typeof(DateTime),1/2/2004,3/4/2004,
ErrorMessage ={0}的值必须在{1}到{2 })]

但是,这表示我输入的任何日期无效!



起初我以为没有拿起英国的日期(当我尝试26/2/2004),但我甚至不能使用日期如2/2/2004。 / p>

我正在使用MVC2中的数据注释,并使用MicrosoftAjax框架进行客户端验证。



任何建议?



谢谢

解决方案

嗯,几年过去了我在MVC4中重新审视了同样的问题我可以告诉你,这显然已经解决了。



我创建了一个非常简单的默认MVC4网站,并给了日期成员以下属性

  [必需] 
[DataType(DataType.Date)]
[Range(typeof(DateTime),1/2 / 2004,3/4/2004,ErrorMessage ={0}的值必须在{1}和{2}之间)]
public DateTime BlogDate {get;组; }

现在,英国数据系统的验证工作正常,不允许日期2/1/2004,允许日期为2004年3月4日或2004年3月26日。



我使用的模板利用了代码优先的EF4,但是我没有理由怀疑它一般没有被修复,因为javascript正常工作。



所以如果你使用MVC2这可能还是一个问题,但是我找到的最佳解决方案是使用MVC4,只要它可用。


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!

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.

I'm using the dataannotations within MVC2, and using the MicrosoftAjax framework for clientside validation.

Any suggestions?

Thanks

解决方案

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.

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

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.

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.

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.

这篇关于英国Datetime字段的数据注释范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆