Click here to Skip to main content
11,922,026 members (57,012 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# MVC3 Razor
I am new to MVC, I am currently using MVC3 with Entity Framwork.
I am trying to Edit a record in my DB,but cannot due to an error that i cannot for the life of me fix.
Below is erro That is beeing thrown when excuting this lone of code

Any assistance will be much appreciated. Thank you Smile | :)

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

        public ActionResult Edit(Ticket ticket)
            if (ModelState.IsValid)
                Ticket ticket1 = db.Tickets.Single(t => t.TicketID == ticket.TicketID);
                ticket.DateCreated = ticket1.DateCreated;
                db.ObjectStateManager.ChangeObjectState(ticket, EntityState.Modified);
                return RedirectToAction("Index");
            //ViewBag.CategoryType_CategoryTypeID = new SelectList(db.CategoryTypes, "CategoryTypeID", "CategoryTypeDesc", ticket.CategoryType_CategoryTypeID);
            ViewBag.HighLevelCategory_HighLevelCategoryID = new SelectList(db.HighLevelCategories, "HighLevelCategoryID", "HighLevelCategoryDesc", ticket.HighLevelCategory_HighLevelCategoryID);
            ViewBag.Priority_PriorityID = new SelectList(db.Priorities, "PriorityID", "PriorityDesc", ticket.Priority_PriorityID);
            ViewBag.ServiceProvider_ServiceProviderID = new SelectList(db.ServiceProviders, "ServiceProviderID", "ServiceProviderName", ticket.ServiceProvider_ServiceProviderID);
            ViewBag.Site_SiteID = new SelectList(db.Sites, "SiteID", "SiteName", ticket.Site_SiteID);
            ViewBag.SubCategory_SubCategoryID = new SelectList(db.SubCategories, "SubCategoryID", "SubCategoryDesc", ticket.SubCategory_SubCategoryID);
            ViewBag.Status_TicketStatusID = new SelectList(db.TicketStatus, "TicketStatusID", "TicketStatusDesc", ticket.Status_TicketStatusID);
            return View(ticket);
Posted 23-Aug-12 5:13am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The error means what it says. Before you add an item to the collection, search the collection to make sure it's not there. If you're doing a bulk insert, you must keep track of what you inserted, which is as easy as keeping a list of strings or ints that represents the keys. Don't insert something twice. If you do, you get this error.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Hi Christian
thanks for the response.

When removing
Ticket ticket1 = db.Tickets.Single(t => t.TicketID == ticket.TicketID);
                ticket.DateCreated = ticket1.DateCreated;

i added date created to the view as a hidden field.
initially it was coming back as null.

@Html.HiddenFor(model => model.DateCreated)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 568
1 Dave Kreskowiak 285
2 Sergey Alexandrovich Kryukov 173
3 Richard MacCutchan 155
4 ppolymorphe 135
0 OriginalGriff 7,036
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,286
3 George Jonsson 2,840
4 Suvendu Shekhar Giri 2,216

Advertise | Privacy | Mobile
Web04 | 2.8.151120.1 | Last Updated 24 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100