日期MVC模型验证 [英] MVC model validation for date

查看:411
本文介绍了日期MVC模型验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有MVC 5的默认验证,我可以设定最低及最新的最大值?

Is there any default validation for MVC 5 where I can set min and max value of date?

在我的模型,我想日期验证

In my model i want date validation

    public class MyClass
    {               
        [Required(ErrorMessage="Start date and time cannot be empty")]
        //validate:Must be greater than current date
        [DataType(DataType.DateTime)]
        public DateTime StartDateTime { get; set; }

        [Required(ErrorMessage="End date and time cannot be empty")]
        //validate:must be greater than StartDate
        [DataType(DataType.DateTime)]
        public DateTime EndDateTime { get; set; }            
    }

*注:根据本 Asp.Net网站时,有一个问题使用日期时间的范围验证,并且不建议。

Ps: According to this Asp.Net Website, there is a problem in using the Range validator for DateTime, and it is not recommended.

请注意:jQuery验证不与Range属性工作,
  约会时间。例如,下面的code将始终显示一
  客户端验证错误,即使日期是在
  指定范围:

Note: jQuery validation does not work with the Range attribute and DateTime. For example, the following code will always display a client side validation error, even when the date is in the specified range:

[范围(typeof运算(日期时间),1966年1月1日,2020年1月1日)]

您需要禁用jQuery的日期验证使用带有日期时间的范围属性。它一般不会去硬编译一个很好的做法
  日期在你的模型,所以使用范围属性和日期时间为
  气馁。

You will need to disable jQuery date validation to use the Range attribute with DateTime. It's generally not a good practice to compile hard dates in your models, so using the Range attribute and DateTime is discouraged.

我也知道,还有的NuGet包如流利的验证和万全,做了验证和检查,如果一个日期比其他更大的伎俩,但我想知道,如果在默认情况下有东西,检查日期的最小和最大值。

I also, know that there are Nuget packages like Fluent Validation and Foolproof that does the trick for validating and checking if one date is greater than other, but I wanted to know if by default there is something to check the date's min and max value.

这是我在什么新消息看到MVC 5.1 ,有支持<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.maxlengthattribute%28v=vs.110%29.aspx\"><$c$c>MaxLength和<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.minlengthattribute%28v=vs.110%29.aspx\"><$c$c>MinLength验证。

From what I saw in Whats new in MVC 5.1, There is support for MaxLength and MinLength validation.

推荐答案

我觉得只是添加属性范围将工作

I think just adding the attribute "Range" will work

[Range(typeof(DateTime), "01/01/1900", "06/06/2079", ErrorMessageResourceType = typeof(Resources.Patient), ErrorMessageResourceName = "DateOfBirth_Range")] 

这篇关于日期MVC模型验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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