英国日期时间字段的数据注释范围

发布于 2024-08-21 20:57:10 字数 623 浏览 5 评论 0原文

我想使用数据注释来验证日期时间字段,但我遇到了问题。根据 MSDN 上的文档 (http://msdn. microsoft.com/en-us/library/system.componentmodel.dataannotations.rangeattribute.aspx),以下内容应该可以完成这项工作

[Range(typeof(DateTime), "1/2/2004", "3/4/2004",
    ErrorMessage = "Value for {0} must be between {1} and {2}")]

但是,这将我输入的任何日期标记为无效!

起初我以为它没有获取英国日期(当我尝试 2004 年 2 月 26 日时),但我什至无法让它使用 2004 年 2 月 2 日等日期。

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

有什么建议吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月如刀 2024-08-28 20:57:10

好吧,几年过去了,我在 MVC4 中重新审视了同样的问题,我可以告诉您,它显然已经得到解决。

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

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

验证现在在英国数据系统下完美运行,不允许日期 2/1/2004,允许日期 4/3/2004 或 26/3/ 2004年。

我使用的模板利用了代码优先的 EF4,但我没有任何理由怀疑它没有得到普遍修复,因为 javascript 也工作正常。

因此,如果您使用 MVC2,这可能仍然是一个问题,但我发现的最佳解决方案是使用 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.

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.

浸婚纱 2024-08-28 20:57:10

据我所知 RangeAttribute 只能在客户端验证数字,您必须编写一个自定义的 javascript 验证器才能使其工作...

查看 http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx 为例关于如何做到这一点。

As far as i know the RangeAttribute can only validate number on client side, you'll have to write a custom javascript validator for this to work...

check out http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx for an example on how to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文