Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a custom validation that seems to work fine, it get hit and the logic is working but when IsValid return false it still go to the create post method

here is my custom validation class
C#
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 false;
            return true;
            
        }
    }


and here is my model
C#
public class IncidentSpares
    {
        //other properties
        [Required]
        [Display(Name="Spare")]
        [CheckIfSparesAvailable(ErrorMessage="Test")]
        public int SpareID { get; set; }
    }


PS: this is a many-to-many table and am trying to validate the Incident class which contain ICollection of IncidentSpare
here is the Incident model
C#
public class Incidents
    {
        //other properties
        [Display(Name="Spares")]
        public virtual ICollection<IncidentSpares> SpareParts { get; set; }
    }


the create method am talking about is in the incidentController

[update]
am trying to make this work on client side I've written GetClientValidationRules method as follows

C#
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
       {
           var rule = new ModelClientValidationRule
           {
               ValidationType = "checkifsparesavailable",
               ErrorMessage = "Spare is not available in stock",
           };
           yield return rule;
       }


do i have to write javascript to make this work in client side, if yes plz help
Posted
Updated 17-Jan-15 23:10pm
v2

1 solution

C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900