与实体框架4个数据注解的日期范围验证 [英] Date range validation with Entity Framework 4 data annotations

查看:502
本文介绍了与实体框架4个数据注解的日期范围验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架4提供一个ASP.NET MVC3 / Razor2 Web应用程序模型。我使用DataAnnotations来实现验证。我需要限制某些日期由SQL SMALLDATETIME类型所接受的范围。

I am using Entity Framework 4 to provide the model for a ASP.NET MVC3 / Razor2 web application. I am using DataAnnotations to implement validation. I need to limit some dates to the range accepted by the SQL smalldatetime type.

我的问题是,我不能得到RangeAttribute为日期字段正常工作。有问题的场模型的元数据的定义是:

My problem is that I can't get the RangeAttribute to work correctly for a date field. The model metadata definition for the field in question is:

    [Display(ResourceType = typeof(Resources.Patient), Name = "DateOfBirth_Name")]
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    [DataType(DataType.Date)]
    [Range(typeof(DateTime), "01/01/1900", "06/06/2079", ErrorMessageResourceType = typeof(Resources.Patient), ErrorMessageResourceName = "DateOfBirth_Range")] 
    public System.DateTime DateOfBirth { get; set; }

使用此code,任何值I投入日期字段,它是由应用程序视为无效。如果其有关,我使用与有关领域的JQuery的UI日期选择器也是如此。

With this code, whatever value I put into the date field, it is treated as invalid by the application. In case its' relevant, I am using the JQuery-UI date picker with the field in question as well.

谁能帮助吗?

推荐答案

您不指定发生错误,但我的猜测是,它是客户端(?)jQuery验证不与<$ C很好地工作$ C> RangeAttribute 。为了验证,禁用jQuery验证与有效输入应该通过(服务器)的验证。

You do not specify where the error occurs, but my guess is that it is client-side(?) jQuery Validation does not work well with the RangeAttribute. To verify, disable jQuery Validation and the valid input should pass the (server) validation.

要解决这个问题,你将不得不写自己的日期范围验证,例如<一href=\"http://blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-net-mvc-3-integrating-with-the-jquery-ui-date-picker-and-adding-a-jquery-validate-date-range-validator.aspx\" rel=\"nofollow\">http://blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-net-mvc-3-integrating-with-the-jquery-ui-date-picker-and-adding-a-jquery-validate-date-range-validator.aspx

To get around this you will have to write your own date range validation, e.g. http://blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-net-mvc-3-integrating-with-the-jquery-ui-date-picker-and-adding-a-jquery-validate-date-range-validator.aspx

另外你可以看看包,如数据注释扩展或的 MVC万无一失验证的,看看他们是否可以用来解决这个问题。

Alternatively you could look into packages such as Data Annotations Extensions or MVC Foolproof Validation to see if they could be used for solving the problem.

这篇关于与实体框架4个数据注解的日期范围验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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