In my application I want to implement the conditional validation and I am new to MVC. My code looks like this.
public class ConditionalValidation : IValidatableObject
{
public bool ValidateName { get; set; }
public String Name { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (ValidateName)
{
if (string.IsNullOrEmpty(Name))
{
yield return new ValidationResult("*");
}
}
}
}
But when I am accessing view of this no validation is working either I checked the checkbox or not and the page is submitting without checking the client side validation.
I checked the ModelState.IsVlaid at the controller but is also true so please suggest where I am doing working.
Thanks