开发者

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

开发者 https://www.devze.com 2023-01-16 23:29 出处:网络
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.

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,开发者_C百科 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号