Bazinga
Bazinga

Reputation: 1014

Why isn't EF 4 updating list?

I am running into a problem where a list object is selected, edited, and then redirected back to the list, but the object is not showing that it has been updated on the list. If the edited object's details view is selected, the editing to the object has been saved. I need the list to update the changes that have been made.

This is the method that the generates the list for the "Index" view:

public static List<MortgageCancellation> DealerSorting(string id,string dealerId)
    {
        var d = from u in db.MortgageCancellations
                where u.DealerID == dealerId
                select u;
        switch (id)
        {
            case "DealID":
                return d.OrderBy(u => u.DealID).ToList();
            case "PrimaryFirstName":
                return d.OrderBy(u => u.PrimaryFirstName).ToList();
            case "PrimaryLastName":
                return d.OrderBy(u => u.PrimaryLastName).ToList();
            case "DateOfApplication":
                return d.OrderBy(u => u.DateOfApplication).ToList();
            case "ProfileStatus":
                return d.OrderBy(u => u.ProfileStatus).ToList();
            case "CurrentStep":
                return d.OrderByDescending(u => u.CurrentStep).ToList();
            case "LastUpdated":
                return d.OrderByDescending(u => u.LastUpdated).ToList();
            default:
                return d.ToList();
        }
    }

This is where the editing takes place, which you can see saves the changes:

public ActionResult Edit(MortgageCancellation mortgagecancellation)
    {
        try
        {
            if (ModelState.IsValid)
            {
                mortgagecancellation.DateOfApplication = (DateTime?)Session["appDate"];
                Session.Remove("appDate");
                mortgagecancellation.LastUpdated = DateTime.Now;
                db.Entry(mortgagecancellation).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(mortgagecancellation);
        }
        catch (Exception ex)
        {
            SupportEmail se = new SupportEmail(error, ex);
            SupportEmail.SendSupportEmail(se);
            return View("Error");
        }
    }

I'm not sure why the "Index" view isn't showing the changes that have been made as this is the only method called from the "Index" view:

return View(MortgageCancellation.OfficeSorting(id));

One more thing, the list eventually shows the changes, but I need the list show the changes immediately.

This is where the db is initialized:

private static MyDBContext db = new MyDBContext();

Upvotes: 0

Views: 88

Answers (1)

AlexT
AlexT

Reputation: 407

try to replace

private static MyDBContext db = new MyDBContext();

with a

using(var db = new MyDBContext())
{
    ...code..
}

for each action.

Upvotes: 1

Related Questions