i have two entity: 1) student and 2) address.
public class Student
{
Public Int StudentId { get; set; }
Public String FullName { get; set; }
Public virtual IList<Address> Addresses { get; set; }
}
public class Address
{
Public Int AddressId { get; set; }
Public Int StudentId { get; set; }
Public String FullAddress { get; set; }
Public virtual Student Student { get; set; }
}
each student may have zero or more address.
i want to create single view for this two entity. i know that must create a view model. this is view model.
public class StudentViewModel
{
Public Int StudentId { get; set; }
Public String FullName { get; set; }
public Address AddAddressModel { get; set; }
Public virtual IList<Address> Addresses { get; set; }
}
and i create a view for StudentViewModel. this is StudentViewModel:
@model MyProject.Models.StudentViewModel
@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm ())
{
@Html.ValidationSummary(true)
<fieldset>
<div class="editor-label">
@Html.LabelFor(model => model.FullName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FullName)
@Html.ValidationMessageFor(model => model.FullName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AddAddressModel.FullAddress)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AddAddressModel.FullAddres)
@Html.ValidationMessageFor(model => model.AddAddressModel.FullAddres)
</div>
<button id="add">add address to list</button>
<input type="submit" value="save in database" />
</fieldset>
please tell me how i can add one or more address on by one in Addresses Property of StudentViewModel and show after this operation to user. finally when i click on the "save in database" button student and his addresses must be inserted in database.