Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6378813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:03:25+00:00 2026-05-25T02:03:25+00:00

I have an MVC 3 project that I am working on using Entity Framework

  • 0

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>
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:03:25+00:00Added an answer on May 25, 2026 at 2:03 am

    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.

    [HttpPost]
    public ActionResult Edit(Employer employer, FormCollection col)
    {
        if (ModelState.IsValid)
        {
            var emp = employer;
            //populate emp.Address and emp.PostalAddress with values from col
            entities.Employers.Attach(emp);
            entities.ObjectStateManager.ChangeObjectState(emp, EntityState.Modified);
            entities.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(employer);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Web project using Asp.Net MVC, which I'll have to deploy
I have an ASP.NET MVC project and I have a single action that accepts
I have an ASP.NET MVC project with a form. In the Action method that
I'm working on a Spring MVC project, and I have unit tests for all
I am in the start up of a project using ASP.NET MVC and have
I am using a Cairngorm MVC architecture for my current project. I have several
I have been using Entity Framework CTP with Code-First as in this tutorial by
The setup: (using Asp.Net MVC 2 RC, Entity Framework, SQL Server, VS2008) My buddy
I'm working on an asp.net-mvc project. I have an Items table and a Tags
I recently joined a team that is working on a ASP.NET MVC project.. they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.