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

  • Home
  • SEARCH
  • 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 6894277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:45:46+00:00 2026-05-27T06:45:46+00:00

I have a db model for which I would like to edit with multiple

  • 0

I have a db model for which I would like to edit with multiple Views. My actual db model is much larger than what I’m showing here.

Db Model: PersonModel

public class PersonModel {
    public Int32 Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Description { get; set; }
}

I then have two Views, EditPersonName.cshtml and EditPersonDescription.cshtml that enable the user to modify different parts of the PersonModel.

View 1: EditPersonName.cshtml

@model EditPersonNameModel
@using (Html.BeginForm()) {
<div class="editor-label">@Html.LabelFor(m => m.FirstName)</div>
<div class="editor-field">@Html.TextBoxFor(m => m.FirstName)</div>
<div class="editor-label">@Html.LabelFor(m => m.LastName)</div>
<div class="editor-field">@Html.TextBoxFor(m => m.LastName)</div>
<input type="submit" value="Save" />
}

View 2: EditPersonDescription.cshtml

@model EditPersonDescriptionModel
@using (Html.BeginForm()) {
<div class="editor-label">@Html.LabelFor(m => m.Description)</div>
<div class="editor-field">@Html.TextBoxFor(m => m.Description)</div>
<input type="submit" value="Save" />
}

Rather than have each view tied directly to PersonModel, I’m wanting link them to EditPersonNameModel and EditPersonDescriptionModel, respectively.

View Model 1: EditPersonNameModel

public class EditPersonNameModel {
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

View Model 2: EditPersonDescriptionModel

public class EditPersonDescriptionModel {
    public string Description { get; set; }
}

I suppose it’s also worth mentioning that I’m using EF’s DbContext pattern.

public class PersonDbContext : DbContext {
    public DbSet<PersonModel> Persons { get; set; }
}

Now for my controller, which is where things start to break!

Controller: PersonController

public class PersonController {
    private PersonDbContext = new PersonDbContext();

    [HttpGet]
    public ActionResult EditName(int id) {
        // ??? ---- #1 -----
    }

    [HttpPost]
    public ActionResult EditName(EditPersonNameModel model, int id) {
        if (ModelState.IsValid) {
            // ??? ---- #2 ----- ??? Update FirstName,LastName ONLY
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(model);
    }

    [HttpGet]
    public ActionResult EditDescription(int id) {
        // ??? ---- #3 -----
    }

    [HttpPost]
    public ActionResult EditDescription(EditPersonDescriptionModel model, int id) {
        if (ModelState.IsValid) {
            // ??? ---- #4 ----- ??? Update Description ONLY
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(model);
    }

}

My problem is that I don’t know what to put in the ??? ---- #X ----- blocks above. Obviously #1 and #3 are going to be very similar, as are #2 and #4.

Some have suggested that I use AutoMapper. That helps going from the Db Model to the View Model, but not the other way around.

Presently my code block for #1 looks like this:

[HttpGet]
public ActionResult EditName(int id) {
    PersonModel person = db.Persons.Find(id);
    if (person == null) {
        return HttpNotFound();
    }
    EditPersonNameModel viewModel;
    AutoMapper.Mapper.CreateMap<PersonModel, EditPersonNameModel>();
    viewModel = AutoMapper.Mapper.Map<PersonModel, EditPersonNameModel>(person);
    return View(viewModel);
}

And my code block for #2 looks like this:

[HttpPost]
public ActionResult EditName(EditPersonNameModel viewModel, int id) {
    if (ModelState.IsValid) {
        PersonModel person = db.Persons.Find(id);
        if (person == null) {
            return HttpNotFound();
        }
        EditPersonNameModel viewModel;
        AutoMapper.Mapper.CreateMap<EditPersonNameModel, PersonModel>();
        person = AutoMapper.Mapper.Map<EditPersonNameModel, PersonModel>(viewModel);
        db.Entry(person).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(viewModel);
}

#1 above actually works fine. AutoMapper will map from the larger Person object to the EditPersonName object.

#2 above, however, is broken. AutoMapper will map from EditPersonNameModel object to a new Person object and thus db.Entry(person).State = EntityState.Modified will throw an exception:

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

I don’t need AutoMapper to create a new object, I need to to map to an existing object. If only there were AutoMapper.Mapper.Update<EditPersonNameModel, PersonModel>(viewModel, person).

Ok, so this is now a very LONG question! Thank you for reading and I really appreciate your help! If you are familiar with AutoMapper and know that I’m doing something wrong, please let me know. If you read this code and know of a different way to do this altogether, please let me know that too!

Thank you!!!


Update 1

I have a #2 working, but I’m not sure how good it is.

New #2

[HttpPost]
public ActionResult EditName(EditPersonNameModel viewModel, int id) {
    if (ModelState.IsValid) {
        PersonModel person = db.Persons.Find(id);
        if (person == null) {
            return HttpNotFound();
        }
        TryUpdateModel(person);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(viewModel);
}

Note that TryUpdateModel is called on person rather than viewModel. I’ve already fetched person from the database, so it’s completely filled out, with an Id. The TryModelUpdate method uses an IModelBinder to map properties from the current “mvc request context” to the specified object. Since my PersonModel and EditPersonNameModel use the same named properties, it works. This is really a very poor solution, so if you know of something better, please let me know!

  • 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-27T06:45:47+00:00Added an answer on May 27, 2026 at 6:45 am

    I’d recommend using Automapper or similar solution, there is no built-in support for model->model mapping in mvc 3.

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

Sidebar

Related Questions

I have various models of which I would like to keep track and collect
Here's what I'm trying to do, and failing... I have a File model which
I have multiple models which relate back to a single model. On save of
I have a model called Category which looks like this: class Category < ActiveRecord::Base
I have an existing ASP.NET website which I would like to painlessly add CMS
I would like to access profiles table from my Profile model which belongs_to :user
I have a model (share) in which I save who can edit a lesson.
I would like to mix a field into an existing model which I would
I have model Foo which has field bar. The bar field should be unique,
I have a model which gets its data from a parser object. I'm thinking

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.