What is the best way of adding “Different than zero” validation attribute in ASP.NET MVC 3/4?
What is the best way of adding Different than zero validation attribute in ASP.NET
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I would assume you would want to add a custom validation attribute with the error message “Different than zero” (not sure of the correct English usage here). Say you have a model with a property that requires the validation:
Create a class
CheckZeroAttributethat derives fromValidationAttributeand override one of theIsValidmethods provided by the base class. Overriding theIsValidversion taking aValidationContextparameter provides more information to use inside theIsValidmethod (theValidationContextparameter will give you access to the model type, model object instance, and friendly display name of the property you are validating, among other pieces of information).