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 6174555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:46:24+00:00 2026-05-23T23:46:24+00:00

I have two POCO classes a User and an Address. Address is a complex

  • 0

I have two POCO classes a User and an Address. Address is a complex object, and a User has one Address. I want to create a single User view that allows the create/edit of an Address on the same form via MVC Scaffolding + Entity Framework + Repository Pattern. The Create User View works correctly, but when I try to make changes to the Address in the User Edit View, the changes don’t get propagated. How do I force the changes to propogated down to the Address object from the User View? Everything else is auto generated.

POCO Classes

public class TestDB : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    }

    public DbSet<Address> Addresses { get; set; }
    public DbSet<User> Users { get; set; }
}

public class Address
{
    public int ID { get; set; }
    public string Street1 { get; set; }
    public string Street2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }
}

public class User
{
    public int ID { get; set; }
    public string UserName { get; set; }

    /*[ForeignKey("Address")]
    public int AddressID { get; set; }*/
    public virtual Address Address { get; set; }
}

Edit User View

<% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true) %>
<fieldset>
    <legend>User</legend>

    <%: Html.HiddenFor(model => model.ID) %>
        <%: Html.Partial("CreateOrEdit", Model) %>
    <legend>Address</legend>
    <div class="editor-label">
        <%: Html.LabelFor(model => model.Address.Street1) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Address.Street1)%>
        <%: Html.ValidationMessageFor(model => model.Address.Street1)%>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Address.Street2)%>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Address.Street2)%>
        <%: Html.ValidationMessageFor(model => model.Address.Street2)%>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Address.City)%>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Address.City)%>
        <%: Html.ValidationMessageFor(model => model.Address.City)%>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Address.State)%>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Address.State)%>
        <%: Html.ValidationMessageFor(model => model.Address.State)%>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Address.PostalCode)%>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Address.PostalCode)%>
        <%: Html.ValidationMessageFor(model => model.Address.PostalCode)%>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Address.Country)%>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Address.Country)%>
        <%: Html.ValidationMessageFor(model => model.Address.Country)%>
    </div>
    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
<% } %>

<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>

Editor Template for Address:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<AddressTest.Models.Address>" %>

<script src="<%: Url.Content("~/Scripts/jquery-1.5.1.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>

<% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true) %>
<fieldset>
    <legend>Address</legend>

    <%: Html.HiddenFor(model => model.ID) %>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Street1) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Street1) %>
        <%: Html.ValidationMessageFor(model => model.Street1) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Street2) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Street2) %>
        <%: Html.ValidationMessageFor(model => model.Street2) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.City) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.City) %>
        <%: Html.ValidationMessageFor(model => model.City) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.State) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.State) %>
        <%: Html.ValidationMessageFor(model => model.State) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.PostalCode) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.PostalCode) %>
        <%: Html.ValidationMessageFor(model => model.PostalCode) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Country) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Country) %>
        <%: Html.ValidationMessageFor(model => model.Country) %>
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
<% } %>

<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>

Change to User Edit View:

<% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true) %>
<fieldset>
    <legend>User</legend>

    <%: Html.HiddenFor(model => model.ID) %>
        <%: Html.Partial("CreateOrEdit", Model) %>
    <%: Html.Editor("Address", Model.Address) %>

    <p>
        <input type="submit" value="Save" />
    </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-23T23:46:26+00:00Added an answer on May 23, 2026 at 11:46 pm

    The solution was to modify the User Update Repository Method and mark the entity state modified for the Address. I also needed to add a hidden property for the Address.ID on the User Edit View.

    User Insert/Update Repository Method

        public void InsertOrUpdate(User user)
        {
            if (user.ID == default(int)) {
                // New entity
                context.Users.Add(user);
            } else {
                // Existing entity
                context.Entry(user.Address).State = EntityState.Modified; // Update Address
                context.Entry(user).State = EntityState.Modified;
            }
        }
    

    Additional Hidden Property on User Edit View

    <%: Html.HiddenFor(model => model.ID) %>
    <%: Html.HiddenFor(model => model.Address.ID) %> <!-- Added this line! -->
    

    Controller Edit Action

        // POST: /User/Edit/5
    
        [HttpPost]
        public ActionResult Edit(User user)
        {
            if (ModelState.IsValid) {
                userRepository.InsertOrUpdate(user);
                userRepository.Save();
                return RedirectToAction("Index");
            } else {
                ViewBag.PossibleAddresses = addressRepository.All;
                return View();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, and want to include a static instance of one class
I have two spreadsheets... when one gets modified in a certain way I want
I have two tables in my database which have a one-to-one relationship. I want
I have two simple POCO classes; I'm trying to get the MyY property below
Have two UIBarButtonItems want to make it as one UIBarButtonItem and toggle between them
I have two applications written in Java that communicate with each other using XML
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two threads, one needs to poll a bunch of separate static resources
I have two webapplication, one is a simple authenticationsite which can authenticate the logged
I have two POCO objects: public class Product { public int ProductID { get;

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.