I'm new to ASP.NET MVC and I want to create a view where I can create a new object along with the related objects.
As example: I have the following class Person:
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual List<Address> Addresses { get; set; }
}
And the class Address:
public class Address
{
public int AddressId { get; set; }
public string City { get; set; }
public string Street { get; set; }
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
What I want to achieve is having a view where the user can type in the data for the person and any number of addresses for this user.
My first (simple) attempt was simply providing an action link which is mapped to a controller method. This method takes a Person object as a parameter, adds a Address object to the collection Addresses and returns a new create view (maybe not nice, but this worked). But when I tried to add a second address I noticed that the Person's collection of addresses was empty again.
Any sugesstions/best practices for this kind of problem?
Thanks!