2013-05-17 93 views
0

當我試圖返回的實體JSON和我不斷收到此錯誤:錯誤返回實體JSON

的ObjectContext的實例已經設置,不能再用於需要連接的操作。

這裏是我的代碼:

public JsonResult ListAllDepartments() 
{ 
    JsonResult jsonResult = null; 
    using (var db = new EventTrackerDB()) 
    { 
     var foundDepartments = from departments in db.EVNTTRKR_Departments 
            select departments; 
     jsonResult = Json(foundDepartments.ToList(), JsonRequestBehavior.AllowGet); 
    } 

    return jsonResult; 
} 

我不明白爲什麼錯誤發生。我在結果集上調用了toList()。

如果我去myapp/Departments/ListAllDepartments,我收到錯誤消息。

有沒有人知道這個解決方案?

謝謝!

+0

嘗試'var foundDepartments = db.EVNTTRKR_Departments..ToList(); jsonResult = Json(foundDepartments,JsonRequestBehavior.AllowGet);' – user2031802

回答

2

一切都得到安置

public JsonResult ListAllDepartments() 
{ 
    using (var db = new UsersContext()) 
    { 
     var foundDepartments = from departments in db.EVNTTRKR_Departments 
          select departments; 
     return Json(foundDepartments.ToList(), JsonRequestBehavior.AllowGet); 
    } 
} 

希望它能幫助你!