Please see this pic:

I have a one to many relationship between eg. Person and Address. Problem here is that it is a “add” form and so even master record is not in database. How should I design this UI and implementation? What if the person clicks “save address” in popup. What personId should I associate that particular address with? In Asp.Net I would have stored it in ViewState and so this problem wouldn’t have come. But how do I do this Asp.Net MVC 3.0? Should I store it in Session and then when finally “save” is clicked on main form I retrieve all address from session and save it. This looks to be a very hackish approach because what if the person open same form in two tabs? Should I keep creating hidden fields on the child form and store it there in CSV format or something? This works but will result in an ungly javascript code.
I hope I am clear on my question. If not please let me know. How do you handle this in Asp.Net MVC?
Thanks.
When the person clicks “Save Address” in the popup, use some javascript to add the entry to the underlying main page, both as text and a set of hidden fields containing the data. When the form is finally posted back, those hidden inputs can be turned into the related data for the main record.
It will look something like this when you are done.
Note this assumes your view model has a
List<Address>namedAddresseswith the appropriate properties on theAddressclass. Note that the indices need to be consecutive. You can use a bit of javascript to update these upon form submit.Note also that if you do this sort of thing when editing and use something similar, you will probably want to add a
Deletedproperty to theAddressclass and merely hide the address being deleted, while setting the correspondingDeletedinput to valueTrueso that you know it should be removed on post. I’ve shown it here without indices as on create they will be bound in the order they occur. On edit if you add one, you may need to calculate the correct index for the new elements.