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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:02:59+00:00 2026-05-23T12:02:59+00:00

i like the .NET MVC practice of getting an instantiated class model passed to

  • 0

i like the .NET MVC practice of getting an instantiated class model passed to the controller which already has some of its properties filled in from posted form values. very handy.

[HttpPost]
public ActionResult DoTask(MyModel model)

model arrives with its id and other properties set from the posted form data.
however, suppose i want to save the model and then access additional properties that exist only in the database. i need to reload the model at this point.

db.Entry(model).State = EntityState.Modified;
db.SaveChanges();
db.Entry(model).Reload();
//model.relatedModel is empty, as are all other model fields not set by the post

i would expect this to reload the model entirely from the database based on its id. but this does not happen for me. the model properties do not get loaded from the database. they are still empty.

EDIT

i do not understand why db.Entry(model).Reload(); is failing for me. but further reading is beginning to suggest that i should dispose of the dbcontext after performing the save and then reloading the model. is this true?

EDIT

WHOOPS! i have it all wrong. i now see that when i commit the changes db.SaveChanges() i am actually overwriting the entire model, not just the properties that have been set by the MVC controller. i must have the wrong pattern. how do i apply the new values (as passed into my controller) to my database without overwriting the non-modified properties? this is turning into a very nube question, i guess.

CONCLUSION

as it turned out, Reload() DOES update as expected. my problem was that I was overwriting my db row when i called a SaveChanges on my dbcontext using a model that was auto-instantiated by the controller context and passed as a parameter to my controller method. in that case it overwrote both the assigned properties as well as the unassigned ones.

i am now doing a select

model = db.Models.Single(x => x.ID == modelID);

and then updating it using UpdateModel (calling with a prefix to make it work)

UpdateModel(model, "myPrefix");
db.SaveChanges();
  • 1 1 Answer
  • 1 View
  • 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-23T12:03:00+00:00Added an answer on May 23, 2026 at 12:03 pm

    If you need to update only selected properties you must do it manually:

    db.YourEntitySet.Attach(model);
    db.Entry(model).Property(m => m.SomeProperty).IsModifed = true;
    // other properties here
    db.SaveChanges();
    

    After this your reload will work but because you want to reaload the model anyway you can do simply this:

    var dbModel = db.YourEntitySet.Single(m => m.Id == model.Id);
    dbModel.SomeProperty = model.SomeProperty;
    // other properties here
    db.SaveChanges();
    

    And you dbModel as the “reloaded” one. You cannot use any authomatic approach like UpdateModel or applying current values because it always overrides everything including values which were not set by your client – EF doesn’t know what is valid change.

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

Sidebar

Related Questions

I got a ASP.NET MVC controller like this [Authorize] public class ObjectController : Controller
It looks like the final release for ASP.NET MVC 2 has been already around
In an ASP.NET MVC application (which has very little chance of having its view
What would be best practice to localize your ASP.NET MVC application? I would like
ASP.NET MVC 2 will support validation based on DataAnnotation attributes like this: public class
I'd like my ASP.NET MVC application to redirect failed requests to matching action methods
In ASP.NET MVC (default routing),I'd like to use a URL like this to return
In ASP.NET MVC I could define a textBox editor like this and give it
I really like the MVC way and have actually enjoyed learning ASP.NET MVC (I
We'd like to use the ASP.Net MVC Framework to extend (and gradually convert) an

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.