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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:56:09+00:00 2026-06-12T17:56:09+00:00

I have the following code: model = new Option(); model.val1 = newVal1; model.val2 =

  • 0

I have the following code:

model = new Option();
model.val1 = newVal1;
model.val2 = newVal2;
model.val3 = newVal3;

//this saves new record just fine
if (recordCount < 1)
{
    context.Options.AddObject(model);
    context.SaveChanges();
}
else
{
    var tempID = from s in context.Options where (s.val1 == newVal1 && s.val2 == newVal2) select s.ID;
    var resultsID = tempID.First();
    model = context.Options.Single(m => m.ID == resultsID);
    if (TryUpdateModel(model, new[] { "val3" }))
    {
       //this isn't updating the record
       context.SaveChanges();
    }
}

The database adds a new entry just fine, but isn’t updating it. What am I missing? 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-06-12T17:56:11+00:00Added an answer on June 12, 2026 at 5:56 pm

    Looking at this code, you first make a new model and set some properties on it:

    model = new Option();  // <- A
    model.val1 = newVal1;
    model.val2 = newVal2;
    model.val3 = newVal3;
    

    then, assuming you’re heading down the “else” path you do this:

    var tempID = from s in context.Options where (s.val1 == newVal1 && s.val2 == newVal2) select s.ID;
    var resultsID = tempID.First();
    model = context.Options.Single(m => m.ID == resultsID);  // <- B
    if (TryUpdateModel(model, new[] { "val3" }))
    {
       //this isn't updating the record
       context.SaveChanges();
    }
    

    which goes out and finds the entry in context.Options that has the matching ID.

    So, now that model, which you created via the new() call (which I’ve marked with the comment “A”) is now cast adrift and you’ve got a different one – the one you retrieved via the call to context.Options.Single(), which I’ve marked with the comment “B”. It has properties based on what’s in the context, not what was in that object you made. That A object is gone now. You’ve got a new object, B, retrieved from the DB.

    So now, you’re calling TryUpdateModel on this retrieved object, telling it that val3 is updated, but the value hasn’t changed, right? It’s whatever you pulled from the context.

    So, it’s not going to update anything because the model object isn’t the one you think it is… the one you updated is waiting to be garbage collected. The one you retrieved hasn’t been updated because it still has whatever value it’s got for the property val3.

    Assuming I follow what you’re trying to do here, that’s why you’re not seeing any updated values in the context.

    If you want to change the value of the val3 property on that model object you’ve retrieved, you need to set it after you retrieve it, otherwise it is overwritten.

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

Sidebar

Related Questions

I have the following code in a view: <div class=wrap_select> @Html.DropDownList(dateRange, new SelectList(Model.DateRange, Value,Text),
I have the following code - var options = new Ext.data.JsonStore({ model: 'options_model', data:
I have the following @Ajax.ActionLink code: @Ajax.ActionLink(Assign Ownership, AssignOwnership, new { techLogCode = Model.TechLog.Code,
I'm developing application using backbone.js & jquery. I have following code in model: runReport:
I have the following code in my model: attr_accessor :expiry_date validates_presence_of :expiry_date, :on =>
I have code like following: switch(sort.Column) { case code: model = (sort.Direction == SortDirection.Ascending)
i have the following code in the rails company model: class Company include Mongoid::Document
I have the following code to generate a table listing brand/model/submodel/style for a group
I have following line code in my view: <td> Model.some_instance_method(args) </td> I would like
In my view I have the following code: @using (Html.BeginForm()) { @Html.TextBox(Date2, Model.Date2) <br/>

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.