FoolProof MVC GreaterThan not working with time

870 Views Asked by At

I have added the unobtrusive.. etc all works except if i have GreaterThan on my second date field

StartDate: 2017/02/25 12:00 AM End Date: 2017/02/25 01:03 PM

it seems to think that the End Date is not greater, i assume its doing a string comparison vs a proper date because that is a valid range as the end date is greater than the start but if we are doing a string compare of that then the end date is not greater

here is my code

    [Required]
    [DataType(DataType.DateTime)]
    [Display(ResourceType = typeof(DisplayResources), Name = "StartDateTime")]
    public DateTime StartDateTime { get; set; }

    [Required]
    [DataType(DataType.DateTime)]
    [Display(ResourceType = typeof(DisplayResources), Name = "EndDateTime")]
    [GreaterThan("StartDateTime")]
    public DateTime EndDateTime { get; set; }

`

update this is the component i am using https://eonasdan.github.io/bootstrap-datetimepicker/ which uses moment.js for its date formats

1

There are 1 best solutions below

0
BKas Development On

I think this works for you.

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (StartDateTime> EndDateTime)
       {
            yield return
         new ValidationResult(errorMessage: "End date must be greater than Start date",
                             memberNames: new[] { "EndDateTime" });
        }

    }