I'm developing a "Create" view for adding a new Person record in the database. In addition to displaying entry fields for Person, the view also needs to allow user to add one or more of Addresses. For example, this is the simplified entity relationship:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public IEnumerable<Address> Addresses { get; set; }
}
public class Address
{
public string Street{ get; set; }
public string City { get; set; }
public string State { get; set; }
}
I know I can do something like this using Razor views:
In the main view Views\Person\Create.cshtml
:
@model Person
@Html.TextBoxFor(model => model.FirstName)
@Html.TextBoxFor(model => model.LastName)
@Html.EditorFor(model => model.Addresses)
And then in Views\Person\EditorTemplates\Address.cshtml
:
@model Address
@Html.TextBoxFor(model => model.Street)
@Html.TextBoxFor(model => model.City)
@Html.TextBoxFor(model => model.State)
But this works well when you're feeding it a populated list of addresses (like maybe in EditPerson view). But I'm not sure how to work with this in the CREATE situation, because the number of addresses is unknown. I guess I could use jquery to generate a new row of input textboxes with careful naming for NAME attribute, but that seems messy.
Anybody worked with submitting a form with one-to-many relationships like this?