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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:13:17+00:00 2026-05-24T02:13:17+00:00

I have this classes who is mapped using Entity Framework Code First: public class

  • 0

I have this classes who is mapped using Entity Framework Code First:

public class Person
{
    public int PersonId { get; set; }
    public string Name { get; set; }
    //... additional properties
    [Required]
    public virtual ICollection<Address> Addresses { get; set; }
}
public class Address
{
    public int AddressId { get; set; }
    public string Street { get; set; }
    //... additional properties

    public int PersonId { get; set; }
    public virtual Person Person { get; set; }
}

So, using ASP.NET MVC 3, how can I perform the Edit View for Person to fill all Person properties within an Address in just one Form?

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Person</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Adresses.FirstOrDefault().Street)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Adresses.FirstOrDefault().Street)
            @Html.ValidationMessageFor(model => model.Adresses.FirstOrDefault().Street)
        </div>

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

This works fine to show the inputs with Street from database, but when I do the submit, the Street doesnt change.

[HttpPost]
public ActionResult Edit(int id, FormCollection formCollection)
{
    Person person = unityOfWork.PersonRepository.GetById(id);

    UpdateModel<Person>(person);

    if (ModelState.IsValid)
    {
        unityOfWork.PersonRepository.Update(person);
        unityOfWork.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(person);
}
  • 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-24T02:13:18+00:00Added an answer on May 24, 2026 at 2:13 am

    Because of the way the model binder has to fix up collections, it will be struggling because the relevant ID’s aren’t present.

    It would be better to create a ViewModel for this screen and translate between it and the underlying domain model.

    If you were providing a facility to edit all addresses, I’d use Steve Sanderson’s BeginCollectionItem helper. But since you’re only editing a single address, a new ViewModel class with a Person property and a separate Address property would be easier, or use a class that included the properties from Person and Address that are relevant to this view.

    I’m a big proponent of the Separation of Concerns. There are logically three models in your application; the Entity model that describes the implementation of data storage in your database, the View model that describes the implementation of data representation in the UI and the Domain model which is the logical representation of the data within your application. It is tempting to use the same model for each purpose, as you’re doing here. But as soon as your requirements become none-trivial, it becomes a burden, so you’re better off creating classes that work best where you need them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some classes layed out like this class A { public virtual void
I have those two classes ///////////BASE CLASS class Base{ public: Base(int = 0); ~Base();
I have some classes extended this way: class Baseresidence extends CActiveRecord { public static
I have two classes, Foo and Bar, that have constructors like this: class Foo
I have two classes declared like this: class Object1 { protected ulong guid; protected
I have the following classes: public abstract class BaseClass { private readonly double cachedValue;
I have some legacy code with linq2sql classes. DataContext class has following definition for
I have a class MyClass and an inner class MyNestedClass like this: public class
I have this interface: public interface ISectionListItem { public int getLayoutId(); public void setProps(View
I have seen this in a lot of XML comments for classes in the

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.