My model is a person contains a collection of another persons(Parents). Every person have a collection of contacts.
Edit.cshtml
@model Person
@using (Html.BeginForm("Save", "", FormMethod.Post, ...))
{
@foreach (var parent in Model.Parents)
{
Html.RenderPartial("ParentItem", parent);
}
@foreach (var contact in Model.Contacts)
{
Html.RenderPartial("ContactItem", contact);
}
\\other
}
ContactItem.cshtml
@model Contact
@using (Html.BeginCollectionItem("Contacts"))
{
@Html.HiddenFor(model => model.Id)
\\other
}
ParentItem.cshtml
@model Person
@using (Html.BeginCollectionItem("Parents"))
{
@Html.HiddenFor(model => model.Id)
@foreach (var contact in Model.Contacts)
{
Html.RenderPartial("ContactItem", contact);
}
\\other
}
The problem appears when I add a new parent to a person (Person.Parents.first now exists), then add a Contact to a parent(Person.Parents.first.Contacts.first expected), model comes to "Save" inside an outer collection (Person.Contacts.last my new contact here) I think it is connected with naming of collections on the form(BeginCollectionItem("Contacts")), but how to solve it?