public class CheckIfSparesAvailableAttribute : ValidationAttribute
{
public CheckIfSparesAvailableAttribute() : base() { }
public override bool IsValid(object value)
{
int quantity, spareID = (int)value;
quantity = new Spares().CheckQuantity(spareID);
if (quantity < 1){
return new ValidationResult("Please select an option.");
}
else{
return ValidationResult.Success;
}
}
}
and since you are using validationattribute, this will hit the controller action method, and in the action method you need to check for the if(ModelState.IsValid)
Because in the custom validation attribute, you are overrriding the IsValid method. You are trying to use the
CheckIfSparesAvailableAttribute
as an attribute, as AuthorizationAttribute is used. Since in this you are overriding the IsValid method, just you need to return as above and check in the Action for ModelState.IsValid
I hope you get what I tried to explain.
For more info. follow:-
Creating Custom Validation Attribute in MVC 3[
^]
Thanks.