Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 MVC 3客户日期验证(在另外两个日期之间)_Asp.net Mvc 3_Validation - Fatal编程技术网

Asp.net mvc 3 MVC 3客户日期验证(在另外两个日期之间)

Asp.net mvc 3 MVC 3客户日期验证(在另外两个日期之间),asp.net-mvc-3,validation,Asp.net Mvc 3,Validation,我有一个约会时间,需要确认一下。。。 我需要我的DateTime停留在另外两个DateTime(动态)之间 我该怎么做呢?您可以实现。我喜欢使用。它使您能够更好地控制验证 使用FluentValidation,您可以执行以下操作(其中MyDateTime、date1和date2是MyModel类的属性): 公共类MyModelValidator:AbstractValidator{ 公共MyValidator(){ (x=>x.MyDateTime)的规则。包括两个(日期1、日期2); } }

我有一个约会时间,需要确认一下。。。 我需要我的DateTime停留在另外两个DateTime(动态)之间

我该怎么做呢?

您可以实现。

我喜欢使用。它使您能够更好地控制验证

使用FluentValidation,您可以执行以下操作(其中MyDateTime、date1和date2是MyModel类的属性):

公共类MyModelValidator:AbstractValidator{
公共MyValidator(){
(x=>x.MyDateTime)的规则。包括两个(日期1、日期2);
}
}

只需声明一个自定义ValidationAttribute

   public sealed class Date1Attribute : ValidationAttribute
{
    public string date2Property { get; set; }
    public override bool IsValid(object value)
    {
        // Compare the date
        string date2String = HttpContext.Current.Request[date2Property];
        DateTime date1 = (DateTime)value;
        DateTime date2 = DateTime.Parse(date2String);

        return date1 >= date2;
    }
}
或者,如果您使用的是日期选择器,您可以像这样限制minDate和maxDate:

   $('#date1').datepicker({
    constrainInput: true, 
    dateFormat: 'D, dd M yy',
    // Once change, set date2 min date
    onSelect: function (dateText, inst) {
        $('#date2').datepicker("option", "minDate", dateText);
       }
    });

   $('#date2').datepicker({
    constrainInput: true, 
    minDate: 0,
    dateFormat: 'D, dd M yy'
   });

希望获得此帮助:)

注意:IValidatableObject不支持客户端验证这一点很好。另一个选项是自定义验证属性,它可以支持客户端验证。。。我在课堂上尝试了流畅的验证。。。它使用NotEmpty()方法显示erros(客户端验证)很好。。。。但是usign RuleFor(x=>x.MyDate).GreaterThan(new DateTime(2010,6,21)).LessThan(new DateTime(2010,12,17))没有进行客户端验证。。。在这种情况下,我如何显示验证消息?@Paul-是的,GreaterThan,LessThan,并且必须没有客户端验证等效项。您可以使用.inclusiveBeween,这应该可以在客户端使用。谢谢。。。但现在我这样做了:RuleFor(x=>x.MyDate)。包括ebetween(newdatetime(2010,6,21),newdatetime(2010,12,17))。带有消息(“错误”);无论我选择什么日期,错误总是被触发。。。有什么想法吗?@Paul我也有同样的问题!注意:datepicker并不阻止您键入超出该范围的日期,它只是在picker中禁用不在该范围内的日期。此外,还必须手动实现客户端验证。
   $('#date1').datepicker({
    constrainInput: true, 
    dateFormat: 'D, dd M yy',
    // Once change, set date2 min date
    onSelect: function (dateText, inst) {
        $('#date2').datepicker("option", "minDate", dateText);
       }
    });

   $('#date2').datepicker({
    constrainInput: true, 
    minDate: 0,
    dateFormat: 'D, dd M yy'
   });