I would like to dinamically add and remove items from a POCO Object and store it on a related table. for instance:
class Contact{
[Key]
public int id {get; set;}
[Required] public string name {get; set;}
public IEnumerable<Phone> phones{get; set;}
}
class Phone{
[Key]
public int id {get;set;}
[Required] public string phone_type {get; set;}
[Required] public string phone_number {get; set;}
}
where Contact is able to have a lot of phones.
The Contact Edit View is like this:
@model SGD.Models.Contact
@Html.LabelFor(model => model.name)
@Html.EditorFor(model => model.name)
@Html.LabelFor(model => model.phones)
<div id="phones"></div>
<a href="#" onclick="addPhone()">Add new phone</a>
<script>
function addPhone() {
$.ajax({url: "/Phones/_Phone",
cache: false,
success: function(html) { $("#phones").append(html);}}
)}
</script>
the "Add new phone" link button append this partial view inside #phones div
@model SGD.Models.Phone
@{
Layout = null;
}
@Html.HiddenFor(model => model.id)
@Html.LabelFor(model => model.phone_type)
@Html.EditorFor(model => model.phone_type)
@Html.ValidationMessageFor(model => model.phone_type)
@Html.LabelFor(model => model.phone_number)
@Html.EditorFor(model => model.phone_number)
@Html.ValidationMessageFor(model => model.phone_number)
But I Don't know how to handle and bind the post data in the controller and store the contact and the related phone records on DB. How can I do it?