I have an MVC project and I have a case where i need to update a parent and multiple child entities at the same time. In the post action I am receiving "an attempt was made to remove a relationship between x and x however one of the relationship's foreign keys" which is strange, all I'm doing is update, I'm not droping any entity whatsoever. I am using Linq to SQL and MVC3. The pseudocode is like the following:
@model Project.Models.ParentModel
...
@using (Html.BeginForm()) {
@Html.Label("Parent property")
@Html.EditorFor(model => model.ParentProperty)
@foreach (var child in Model.Childs)
{
Html.RenderPartial("_EditChild", child)
// Which is nothing more than a label and an editor for a property like:
// @model Project.Models.ChildModel
// @Html.Label("Child property")
// @Hteml.EditorFor(model => model.ChildProperty)
}
...
}
The Action looks like:
public ActionResult Edit(int id, FormCollection collection)
{
var parent = new Parent();
TryUpdateModel(Parent()); // which updates the parent and the child properties correctly
dataContext.SubmitChanges();
}
Can anybody explaing this behavior. Once again, I'm not removing or dropping any child entities!