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.

2
  • Did you said about fails on client-side or server-side and did you check value binding is correct? Commented May 6, 2015 at 14:54
  • its on the client side. it all seems to be bound properly. Commented May 6, 2015 at 15:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.