为什么不是EF 4更新列表?

时间:2012-09-21 22:09:59

标签: c# asp.net-mvc-3 entity-framework-4

我遇到了一个问题,其中列表对象被选中,编辑,然后重定向回列表,但该对象没有显示它已在列表上更新。如果选择了编辑对象的详细信息视图,则已保存对对象的编辑。我需要列表来更新已经做出的更改。

这是为“索引”视图生成列表的方法:

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();
        }
    }

这是进行编辑的地方,您可以看到保存更改:

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");
        }
    }

我不确定为什么“索引”视图没有显示已经进行的更改,因为这是从“索引”视图调用的唯一方法:

return View(MortgageCancellation.OfficeSorting(id));

还有一件事,列表最终显示了更改,但我需要列表立即显示的更改

这是db初始化的地方:

private static MyDBContext db = new MyDBContext();

1 个答案:

答案 0 :(得分:1)

尝试替换

private static MyDBContext db = new MyDBContext();

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

每个动作。