I have an MVC 3 project that I am working on using Entity Framework as my model. I have an object “Employer” that has “Address” and “PostalAddress” that I would like to display (view) and update (edit) at the same time (i.e. on page with employer details and address details being updated at the same time)
My view seems to work fine:
var employer = (from e in entities.Employers.Include("Address").Include("PostalAddress")
where e.EmployerNumber == employerNumber
select e).First();
return View(employer);
My edit shows up fine (i.e. all of the textboxes are populated with both employer and address details)
[HttpPost]
public ActionResult Edit(Employer employer)
{
if (ModelState.IsValid)
{
entities.Employers.Attach(employer);
entities.ObjectStateManager.ChangeObjectState(employer, EntityState.Modified);
entities.SaveChanges();
return RedirectToAction("Index");
}
return View(employer);
}
But when I go to save I get the following exception on the entities.Employers.Attach(employer) line:
A referential integrity constraint violation occurred: The property
values that define the referential constraints are not consistent
between principal and dependent objects in the relationship.
When I look at the employer object it is trying to attach it seems to have “lost” it’s Address and PostalAddress items.
This is my first MVC 3 project so any help would be appreciated.
The edit page view looks like this
@model MyProject.BusinessObjects.Employer
@{ ViewBag.Title = "Edit Employer Details"; }
<h2>Edit Employer Details</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Employer</legend>
<div class="editor-label">Name</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<fieldset>
<legend>Address</legend>
<div class="editor-label">Line One</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.LineOne)
@Html.ValidationMessageFor(model => model.Address.LineOne)
</div>
<div class="editor-label">Line Two</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.LineTwo)
@Html.ValidationMessageFor(model => model.Address.LineTwo)
</div>
<div class="editor-label">Suburb</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.Suburb)
@Html.ValidationMessageFor(model => model.Address.Suburb)
</div>
<div class="editor-label">State</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.State)
@Html.ValidationMessageFor(model => model.Address.State)
</div>
<div class="editor-label">Post Code</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address.PostCode)
@Html.ValidationMessageFor(model => model.Address.PostCode)
</div>
</fieldset>
<p>
<input type="submit" value="Save Changes" />
</p>
</fieldset>
}
I think you are having problems with the ModelBinder.
Its probably not working with complex types for you (I’m guessing Address and PostalAddress are navigational properties of Employer).
you could try the code below.