I can tell it's returning the right message, but it never displays the message. What am I missing?
DateValidator:
TreasuryModel:
TreasuryPopupTemplate:
DateValidator:
using System;using System.ComponentModel.DataAnnotations;namespace PosPayAndBankRec.Models.Validators{ public class Date { public enum ValidationType { RangeValidation, Compare } public class DateValidationAttribute : ValidationAttribute { private ValidationType _validationType; private DateTime? _fromDate; private DateTime _toDate; private string _defaultErrorMessage; private string _propertyNameToCompare; public DateValidationAttribute(string message, string compareWith = "") { _propertyNameToCompare = compareWith; _defaultErrorMessage = message; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var baseProperyInfo = validationContext.ObjectType.GetProperty(_propertyNameToCompare); DateTime startDate; bool NotValid = false; if (baseProperyInfo.GetValue(validationContext.ObjectInstance, null) is DateTime) { startDate = (DateTime)baseProperyInfo.GetValue(validationContext.ObjectInstance, null); if (value != null) { DateTime thisDate = (DateTime)value; if (thisDate <= startDate) { NotValid = true; } } else if (value == null) { //From date as a value but not the to date NotValid = false; } } else { //To date not a date and we have a from date if (value != null) { NotValid = true; } } //return validation result if (NotValid == false) { return null; } else if (NotValid == true) { string message = string.Format(_defaultErrorMessage, validationContext.DisplayName);//, displayAttr.Name); return new ValidationResult(message); } return null; } } }}TreasuryModel:
[Date.DateValidation("Deactivation Date should be greater than the Activation Date.", compareWith: "brActivationDate")]public Nullable<System.DateTime> brDeactivationDate { get; set; }TreasuryPopupTemplate:
<td> @Html.EditorFor(model => model.brActivationDate) @Html.ValidationMessageFor(model => model.brActivationDate) </td> <td> @Html.EditorFor(model => model.brDeactivationDate) @Html.ValidationMessageFor(model => model.brDeactivationDate) </td>