I’ve got an MVC app which features a DropDownList and TextBox. I’m using MVC validation and I’m happy with it.
At the moment The DropDownList is [Required], but I want to make it so the TextBox can function as an ‘other: please specify’ style input for the DropDownList.
How do I make it so that the [Required] attribute on the DropDownList is conditional on the TextBox being empty?
This question is similar, but it’s over a year old. Anything in the current version of MVC that makes this easy to do?
It would be pretty easy to write a custom validation attribute:
then you could have a view model:
A controller:
And of course a view:
or you could do as I do: download and use the FluentValidation.NET library, forget about data annotations and write the following validation logic which seems pretty self-explanatory:
Just go ahead and
Install-Package FluentValidation.MVC3and make your life easier.