I want to return list from my controller to the success function in an AJAX call. I’m able to return the list successfully from controller but inside AJAX call, the code for error gets executed and I get an internal server error. I searched for the same everywhere, but I’m unable to find a reason why this is happening.
Following is my AJAX:
$.ajax({
type: "POST",
dataType: 'JSON',
url: "/BookUtility/GetHallsList",
success: function (hallsList) {
console.log(hallsList);
},
error: function (xhr,status, exception) {
console.log("Error: " + exception + ", Status: " + status);
}
});
Controller:
Hall objHall = new Hall();
[HttpPost]
public JsonResult GetHallsList()
{
var hallsList = objHall.GetHallsList();
return Json(hallsList.ToList());
}
Hall.cs
:
public List<tblHall> GetHallsList()
{
List<tblHall> hallsList;
using (BookingSystemDBEntities db = new BookingSystemDBEntities())
{
hallsList = db.tblHalls.ToList();
}
return hallsList;
}
I’ve also tried using List<tblHall> hallsList = objHall.GetHallsList();
in my controller in place of var hallsList = objHall.GetHallsList();
, but that doesn’t work too.
This is the error I’m getting: Error I received
Where am I going wrong?
As your error says, the problem is with the web api method. It should throw the error . The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. You can fix it by setting
using (var contex = new BookingSystemDBEntities())
{
context.Configuration.LazyLoadingEnabled = false;
hallsList = db.tblHalls.ToList();
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments