forum

Home / DeveloperSection / Forums / One-to-many relationship in the create action method

One-to-many relationship in the create action method

Tom Cruser 1741 05-Dec-2014

I'm currently working on a project that requires add multiple addresses for one client. I have three models: here is a link to my class diagram: http://sdrv.ms/1fWioA2 

Person model: 

public class Person
{   
    public Person()
    {
        this.Adresses = new HashSet<Address>();
    }
 
    public int PersonID { get; set; }
 
    [Required(ErrorMessage = "Name is required")]
    public string Name { get; set; }
 
    [Required(ErrorMessage = "Email Address is required")]
    [DisplayName("Email Address")]
    //[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",
    //ErrorMessage = "Email is is not valid.")]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
 
    public string Mobile { get; set; }
    public string Phone { get; set; }
    public string Fax { get; set; }
 
    public virtual ICollection<Address> Adresses { get; set; }
}

address: 

public class Address
{
    [HiddenInput(DisplayValue = false)]
    public int ID { get; set; }
    public string Street { get; set; }
    public string Building  { get; set; }
 
    public int PersonID { get; set; }
    public int CityID { get; set; }
 
    public virtual City City { get; set; }
    public virtual Person Person { get; set; }
}
I created modelview to bind to my Create view:
 
public class PersonViewModel
{
    public Person Person { get; set; }
    public ICollection<Address> Adresses { get; set; }
}

My question is how to bind the viewmodel to the create view in order to be able to save multiple addresses for the same Person?


Updated on 06-Dec-2014

Can you answer this question?


Answer

2 Answers

Liked By