Needing to copy properties before validation

Posted by Mikael on Stack Overflow See other posts from Stack Overflow or by Mikael
Published on 2010-05-25T08:01:42Z Indexed on 2010/05/25 8:11 UTC
Read the original article Hit count: 309

I have a farily complex model needing to be validated, the problem is that this model is used on two different places, one where you register your customer and one where you simply add addresses. Some fields on the address are simply not visible on the register customer form. So when i check if ModelState.IsValid i get false of course since eg. the name is not entered on the billing address, but it is on the customer. That is why i want to before validation occurs, copy a couple of fields to the model, and then validate. I am somewhat lost though and i need help.

My action looks something like this:

public ActionResult Register(WebCustomer customer) 
{
     customer.CopyProperties();
     if(TryUpdateModel(customer)) 
     {
       ...
     }
     ...

But it always returns false, and ModelState.IsValid continues to be false.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about validation