1

If I have a property in a view model like:

 [DataType(DataType.DateTime)]
 public DateTime? MyDate{ get; set; }

And a validation rule like this:

public class YourDetailsViewModelValidator : AbstractValidator<YourDetailsViewModel>
{
    public YourDetailsViewModelValidator()
    {           
        RuleFor(x => x.MyDate)                
            .InclusiveBetween(startDate, endDate)
               .WithMessage("error");
    }                       
}

Why does the error fire regardless of what date is input?

I did see a similar thing was happening enter link description herebut the answer was ultimately accepted so I'm hoping it can be made to work properly.

Community
  • 1
  • 1
davy
  • 4,474
  • 10
  • 48
  • 71

1 Answers1

0

Error can be explained by difference in jquery.validate plugin date formatting and MVC helpers formatting. Just look in html generated by your view, and use

[DisplayFormat(DataFormatString="...")] // format string instead of dots

to make data-val-min and data-val-max attributes conform input value format (if they not, of course).

If value and validation attributes format are the same, but validation still fails - make sure that jquery.validate has appropriate format. In this case you should change ASP.NET MVC application culture to conform jquery.validate format, or vice versa — change jquery.validate format to conform application.

Similar questions asked on SO:

First

Second

.NET date formatting options available here.

Community
  • 1
  • 1
David Levin
  • 6,573
  • 5
  • 48
  • 80