I'm trying to validate a filed based on another field in a list
public class CustomerModel
{
public List<CustomerBrand> Brands { get; set; }
}
}
public class CustomerBrand
{
public int Priority { get; set; }
public bool Checked { get; set; }
}
As per below if a checkbox is ticked then the value entered in the textbox must be greater than zero. Can anyone help out with a solution that keeps that validation in the model and out of the controller
I have made it work using this method but would like to be able to indicate the offending row.
public class CustomerModel : IValidatableObject
{
public string Name { get; set; }
public List<CustomerBrand> Brands { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
foreach(var brand in Brands)
{
if(brand.Checked)
{
if(brand.Priority < 1)
{
yield return new ValidationResult("Priority must be greater than 1");
}
}
}
}
}