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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:20:10+00:00 2026-06-06T10:20:10+00:00

I have three models that are coming together to create one view model and

  • 0

I have three models that are coming together to create one view model and I’d like to be able to edit that view model when clicking “edit”. I can’t find a straight forward example of how this works (anywhere).

I’m not sure if I’m going down the right path. I am able to get the view with the data. At this point, I am unable to save it.

Any help would be appreciated.

Thanks!

Models:

    public class Person
{
    [Key]
    public int Id { get; set; }

    [MaxLength(20)]
    [Required(ErrorMessage = "First name is required.")]
    public string FirstName { get; set; }

    [MaxLength(20)]
    [Required(ErrorMessage = "Last name is required.")]
    public string LastName { get; set; }
    [MaxLength(40)]
    [Required(ErrorMessage = "Email is required.")]
    public string Email { get; set; }
    [MaxLength(20)]
    [DataType(DataType.PhoneNumber)]
    public string Phone { get; set; }

    public bool Active { get; set; }
}


    public class ClientContact
{
    [Key]
    [ForeignKey("Person")]
    public int ClientPersonId { get; set; }
    public int ClientId { get; set; }
    [MaxLength(40)]
    public string Title { get; set; }

    public Person Person { get; set; }
    [ForeignKey("ClientId")]
    public Client Client { get; set; }
}

    public class Client
{
    [Key]
    public int ClientId { get; set; }
    public string Name { get; set; }
    public bool Active {get;set;}

}

View Model:

    public class ClientContactViewModel
{

    private SimplexDB db = new SimplexDB();


    public ClientContactViewModel()
    { 

    }


    public ClientContactViewModel(int id)
    {
        ClientPersonId = id;
        InitializeClientContact();
    }

    public int ClientPersonId { get; set; }


    [Display(Name = "First Name")]
    public string FirstName { get; set; }
    [Display(Name = " Last Name")]
    public string LastName { get; set; }
    [Display(Name = "Title")]
    public string Title { get; set; }
    [Display(Name = "Email Address")]
    public string Email { get; set; }
    [Display(Name = "Phone")]
    public string Phone { get; set; }
    [Display(Name = "Client Name")]
    public int ClientId { get; set; }


    public SelectList Clients
    {
        get
        {
            return new SelectList(db.Clients, "ClientId", "Name");

        }
    }

    private void InitializeClientContact()
    {
        var contact = db.ClientPersons.Include("Person").Where(x => x.ClientPersonId == ClientPersonId).SingleOrDefault();
        if (contact != null)
        {
            FirstName = contact.Person.FirstName;
            LastName = contact.Person.LastName;
            Title = contact.Title;
            Email = contact.Person.Email;
            Phone = contact.Person.Phone;
            ClientId = contact.ClientId;

        }
    }



}

Controller:

                public class ClientContactController : Controller
    {
        private database db = new database();

//
        // GET: /ClientContact/Edit/5

        public ActionResult Edit(int id)
        {
            return View(new ClientContactViewModel(id));
        }

        //
        // POST: /ClientContact/Edit/5

        [HttpPost]
        public ActionResult Edit(ClientContactViewModel model)
        {
            if (ModelState.IsValid)
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(model);
        }
}

I get an error at the db.Entry(model).State… “The entity type ClientContactViewModel is not part of the model for the current context.”

  • 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-06-06T10:20:11+00:00Added an answer on June 6, 2026 at 10:20 am

    Your ViewModel is not an entity. You should map your ViewModel to your entity, then set the entity’s state to modified.

    Basically, this means that you should set your entity values with your view model values. You can use AutoMapper or handle it manually:

        [HttpPost]
        public ActionResult Edit(ClientContactViewModel model)
        {
            if (ModelState.IsValid)
            {
                ClientContact contact = db.ClientPersons.Include("Person")
                                        .Where(x => x.ClientPersonId == model.ClientPersonId)
                                        .SingleOrDefault();
                contact.FirstName = model.FirstName;
                // etc
                db.Entry(contact).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(model);
        }
    

    See http://lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models/ for an excellent approach to using ViewModels in MVC.

    Also, I would highly recommend not doing any data access in your ViewModel. Do that in your Controller, or even better, in a Repository that is used by your Controller. Model binding doesn’t play well with models that have logic (i.e they shouldn’t contain anything more than simple get/set properties).

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

Sidebar

Related Questions

I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20,
I have three models that I'm trying to setup: Location/Venues, Categories, and Neighborhoods. A
I have category model that has a tree structure. In my database I have
If I have a Tree that has Apples, how should I model the fact
I have three models: class User include Mongoid::Document field :name, :type => String has_many
I have three models: User Image Group . User belongs to an image; Group
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
Here is my problem. I have a list of models that are displayed to

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.