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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:27:33+00:00 2026-05-27T07:27:33+00:00

Updating an object with MVC3 I have a model that I can modify, please

  • 0

Updating an object with MVC3

I have a model that I can modify, please see the sample below:

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

The model has other fields that are not showing in the view and cannot be modified by the user, but when I click the submit button the fields that were not showing in the view got set to null.

Can I somehow let EF know not to modify certain fields? Thanks.

  • 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-27T07:27:34+00:00Added an answer on May 27, 2026 at 7:27 am

    Generally it is better not to bind to the entity object directly, rather create an edit model and bind to that.

    After all.. whats to stop someone posting back values you don’t want changed with this approach?

    The main problem here is the fact that mvc model binding changes the properties in the model before its in a context therefore the entity framework doesn’t know which values have changed (and hence which should be updated)

    You’ve mitigated that slightly with db.Entry(c).State = EntityState.Modified; but that tells the entity framework that the whole record has been updated.

    I would normally do the following:

    1. Bind to a model specifically for this controller first
    2. Create an instance of the entity class you want to update, set the Id accordingly and attach it to the context
    3. Update the properties on the entity to be the same as the model you binded to (object is attached and therefore entity framework is tracking which columns are being changed now)
    4. SaveChanges

    Step 3 is a bit tedious therefore consider using a tool like automapper to make things easier

    Edit:

        [HttpPost]
        public ActionResult Edit(Company c)
        {
            if (ModelState.IsValid)
            {
                Company dbCompayObjct = new Company { companyId = c.companyId };
                db.Company.Attach(dbCompayObjct);
    
                dbCompanyObjct.CompanyName = c.CompanyName;
                dbCompanyObjct.City = c.City;
    
                db.SaveChanges();
    
                return RedirectToAction("Index");
            } 
            return View(c);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript object that I would like to update with data from
I am updating a django model object. After setting value for each attribute when
I have a matlab timer object. It is updating a global variable FOO once
I am having some issues when updating an object that has a nested attribute.
I have been trying to use one View for updating an object and all
I have the following class that's used by my MVC3 application. I would like
I'm having an issue with the UITableView object updating after load. I have a
I have a singleton data manager object that manages the connection to a web
How can I save (insert) only associated objects without saving (updating) the base object?
I am creating an object which tracks changes (Updates) regarding the creation, updating and

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.