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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:57:43+00:00 2026-05-23T01:57:43+00:00

I have an object that I am trying to update using MVC(2), also using

  • 0

I have an object that I am trying to update using MVC(2), also using EntityFramework framework.

I know I haven’t provided much code but don’t really feel it necessary. The reason I have logic like this, i’ll explain:

  • I have some cascading drop downs, and when one of the drop downs is empty I use jQuery to fill it with an “UNKNOWN” value e.g. with id like -1.
  • So when I get value -1, I create the UNKNOWN value which is in another table.
  • Then I find that object and assign it to Fruit

Code like this:

if (id == -1)
            {
                //The object was unknown so create it
                var newUnknown = new Fruit
                                 {
Name = "UNKNOWN";
};

EntityFramework.AddToFruits(newUnknown);
EntityFramework.SaveChanges();
defaultValueObject = EntityFramework.Fruits.Single(x=>x.FruitID == newUnknown.FruitID);

object.Fruit = defaultValueObject;
object.Date = DateTime.Now;

UpdateModel(object);
EntityFramework.SaveChanges();

After UpdateModel(object); line is run, the value I set in, for example, Fruit reverts to what was sent over from the form… (which is -1) and then EntityFramework.SaveChanges(); fails FK contrainst (because fruit with id -1 doesn’t exist)! Fair enough – but that’s not what I assigned to you!

I don’t understand why it reverts, because after the first AddToFruits() the unknown is in the database fine… and all up untill UpdateModel(object); it is in object…

If it adds it like I have assigned it there will be no FK contraint exception. But MVC’s UpdateModel decides to do something strange and default to (perhaps what came over with form submission) and screws it up.

Why does this happen? How can I fix it?

  • 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-23T01:57:44+00:00Added an answer on May 23, 2026 at 1:57 am

    You can pass in an array of strings to UpdateModel to tell it what properties to update in your model.

    public class Fruit
    {
        int ID { get; set; }
        bool IsTasty { get; set; }
        string MyOtherPropert { get; set; }
        DateTime Date { get; set; }
    }
    
    ...
    
    if (id == -1)
    {
        //The object was unknown so create it
        var newUnknown = new Fruit
        {
            Name = "UNKNOWN"
        };
        EntityFramework.AddToFruits (newUnknown);
        EntityFramework.SaveChanges ();
    
        defaultValueObject = EntityFramework.Fruits.Single (x=>x.FruitID == newUnknown.FruitID);
        defaultValueObject.Date = DateTime.Now;
    
        UpdateModel (object, new string[] { "IsTasty", "MyOtherProperty" });
    
        EntityFramework.SaveChanges ();
    }
    

    Doing this will make sure all the properties you want updated will be, and those you want left alone won’t be touched. In this case, ID and Date are going to be ignored by UpdateModel.

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

Sidebar

Related Questions

I have a VB6 dll that is trying to create a COM object using
I have an OLE COM object that trying to write a wrapper for, I
I have a hidden embedded QuickTime object on my page that I'm trying to
I have a java.sql.ResultSet object that I need to update. However the result set
I have a ASP.NET MVC 3 application, using Entity Framework 4 to handle Data
I have an object that is mapped to a cookie as a serialized base-64
I have an object that implements IDisposable that is registered with the Windsor Container
I have an object that needs a test if the object data is valid.
I have an object that starts a thread, opens a file, and waits for
I have an object that implements ArrayAccess , Iterator and Countable . That produces

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.