views:

43

answers:

2

Here is my database design.
Contact
ContactID
ContactName

EmailAddress
EmailID
ContactID
EmailText

In the create View, I have ContactName Field and a button Add Email Address.
When I Click on the Add Email Address, there is a popup who perform an ajax request to get the Address Fields. My problem is that I need to create the Contact before creating address because I need a ContactID first. Anybody know how to proceed ?.


I use asp.net MVC 2 and Entity Framework 4

+2  A: 

When you add your email, are you submiting that to your server? Or are you just passing that data back to the parent window (where contact Name is), and putting it under the name so it's visible before submission?

Another option would be rather than open a popup window, just do some JS Magic and show an email field right below the contact name (or just have an option textbox under Contact Name.

So you'd see:

Contact Name [......]
(Add Email) +

They click + and it goes:

Contact Name [.......]
Email 1:  [.......]  (remove x)
(Add Email) +

On submission you'd have your contact Name and a nice list of emails to go with it. Create your contact and then create your emails. =

Ryan Ternier
A: 

My code does something a bit more complicated, but very similar. What I do is create and write the email entry with a blank ContactID when the dialog is closed. Then when I submit my Contact form, I have a field with the EmailID. In my Contact Save action, I check the Email record and if the contactID field is empty, I insert the ContactID and save the Email record. I use the same partial view for Creating and Editing, so if it's Editing an existing email, or adding a new email to an existing contact, the ContactID field is already filled.

In this simple case, it may make more sense when the users closes the dialog to save the info to hidden fields on your parent form (if you don't want them displayed, if you do want them displayed then Ryan's suggestion is best) and then create and save both records on the Contact submit.

If this is a "Create" then I don't really understand why you need an AJAX request to get values...

RememberME
I use this method here. Thanks for your help
Jean-Francois