How to handle conditional validation in MVC3?

I'm just learning MVC and can't figure out how to handle the following scenario, I have a form with a shipping address and billing address. All fields are required. There is also a checkbox for "Billing address matches delivery address." My problem is that the state of the model is never valid if both addresses are not completed. I could handle this pretty easily with some javascript that just added the values ​​from the delivery address to the billing address fields, but if there is a better way to handle it, I would like to know. Any guideline on how this is usually handled will be great. Thank!

+5
source share
2 answers

There are many ways to handle this. If you are not using client-side validation, you can simply implement IValidatableObject in the model and override the IsValid () method and do some code to verify the model is correct.

If you use client-side validation, you need a custom attribute or use something like smooth validation .

+4
source

This can be done by creating your own validation attribute:

http://blogs.msdn.com/b/simonince/archive/2011/02/04/conditional-validation-in-asp-net-mvc-3.aspx

, ValidationAttribute IClientValidatable, , ( ) .

, , , , .

-, , MVC . , .

+4

All Articles