Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I was asked a question in an interview regarding validation with data annotations in MVC.

I have a drop down list and two text boxes.
If some value is selected from the drop down (say "XYZ"), then the apply validation on textboxes to be required else, textboxes can be left blank on submit.

Note: This shouldn't be handled via jQuery and custom annotations, only through data annotations.

I was left clueless. Is this question a valid one?

Appreciate any help.
Posted

1 solution

Couple of options:

1) Make your model class inherit from IValidatableObject and implement the validate method, see https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.ivalidatableobject.aspx[^]


That though is not using the Data Annotations, but...

2) https://github.com/JaroslawWaliszko/ExpressiveAnnotations[^] allows for a RequiredIF annotation that can compare against other values in the model.
 
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