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?
Last updated:12/6/2014 5:42:08 AM
Anonymous User
For this purpose, create an editor template in /Shared/Editor called Address.cshtml:
Now, in your View, all you need to do is this:
@Html.EditorFor(v => v.Addresses)
This will create form fields like this: