I have two date on my razor view and I need to valdiate on that and using data annotation show the error if To date is less than from date. when I run the code in the debug mode the break point hits but it is not rendering the error on the page. it displays the form again the same way if the model state is not valid Below is my code, am I doing something wrong.
public class AcctViewModel: IValidatableObject
{
[Key] public long AccountId { get; set;}
[Display(Name = "Account Name"), DataType(DataType.Text), StringLength(15), Required]
public string AcctName {get;set;}
[Display(Name = "From Date"), DataType(DataType.Date), Required]
public DateTime FromDate { get; set; }
[Display(Name = "To Date"), DataType(DataType.Date), Required]
public DateTime ToDate { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
List<ValidationResult> errors = new List<ValidationResult>();
if (ToDate < FromDate)
{
errors.Add(new ValidationResult($"{nameof(ToDate)} needs to be greater than From date.", new List<string> { nameof(ToDate) }));
}
return errors;
}
}
<div class="form-row">
<div class="col-md-12">
<div class="form-group form-group--float">
<input class="form-control date-picker" asp-for="ToDate">
<label asp-for="ToDate"></label>
<div class="invalid-tooltip"><span asp-validation-for="ToDate"></span></div>
<i class="form-group__bar"></i>
</div>
</div>
</div>
</div>