views:

159

answers:

1

We are facing a scenario..here it is:

Our Customer Model has collection of Address object. The form displays 3 address for the customer for their input. However only the first address is mandatory. Now our Validation is on the Address Model, something like, "street address cannot be null".

So when the form is submitted, the validation is executed on all the 3 address, even though the second and third address is not mandatory, the validation summary displays the message that "Street address 1 cannot be null" twice.

So how can we make this validation for second and third address optional ? like it validation executes only if the customer enters the second and third address. We want to use only the server side validation.

Or Is there a way in controller that we remove the error message from Model State and forcefully make ModelState.IsValid to true ?

A: 

We have this one too.

You can use standard validation if your addresses are stored in a collection, and you add each address as the user requires, making sure there is always at least one.

Have add and remove address actions, then smarten up your form with JS to make one post when enabled.

Anthony Johnston
Thanks for your response. We dont want to use JS. And all the address (like 3) will be populating at once. Any another approach ?
Khu
the only other way then is complex validation on the parent object and not having Required on any of the address model properties
Anthony Johnston