简体   繁体   中英

asp.net MVC 2: in a create form, how to add new model relation entity with ajax request

First of all , I use MVC 2 with Entity Framework 4.
I Have 2 Entities.
Customers and Emails
There is a Relation 1 to many between Customer and Email. One customer ca have many Email.

My question : In the Customer Creation page form, I have all the info related to the customer. ex:

<%: Html.LabelFor(model = model.FirstName) %>
<%: Html.TextBoxFor(model = model.FirstName) %>

<%: Html.LabelFor(model = model.LastName) %>
<%: Html.TextBoxFor(model = model.LastName) %>


I would like to have an Add Button that will create Email Fields in javascript. When I Save the customer, that will Associate the Email with the customer.
How can I perform this task??

I Have a couple of ideas, but not sure if it's ok the way I think.
One of them is when I Click the Add Button for email, that will show a Popup with the Email Fields, then when I Click Save Email , that will call the Create Method and save the email in the database. The problem here is, how can I associate this email entry with the customer if the customer is not already saved in the database?.

Steve Sanderson has a nice bit of code for this:

http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

I've found it really helpful and I am using it in several places. I use L2S rather than EF4 but It should work just as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM