Return list to ajax call from controller asp.net

Anchal Chaudhary

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?

Sajeetharan

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.

edited at
0

Comments

0 comments
Login to comment

Related

Call Controller Method Which Return View With Ajax Call From Asp.net View Page

How to return a List from controller to Ajax call and print it in the success?

Return Json object from Asp.net webMethod to Ajax call

ASP.Net Core posting list from ajax to controller

Ajax call asp.net mvc 5 controller, parameter return null

Asp.net Core 3.1 - Upload File with List<object> via AJAX call & send it to Controller

Passing custom success/error messages to ajax call from asp.net controller

ASP.NET Core MVC controller receives null for input parameter from ajax call

How to call method of a controller from View without using ajax in ASP.NET MVC?

ASP.NET controller does not receiving data from Ajax POST call

Reading in view return values ​from the controller AJAX / ASP.NET Update NEW

Parse return value from $ajax call in JavaScript from MVC Controller

ASP.Net Core Call a controller from another controller

How can I return a list of data from my json file to my controller file in ASP.NET?

C# & ASP.NET MVC Razor - return checked items in list from view to the controller

Asp.net MVC Ajax call not calling controller method

Making a Simple Ajax call to controller in asp.net mvc

Ajax call controller with Insert query ASP.NET

ASP.NET Core - Call controller method in javascript without Ajax

Some value are null AJAX call to controller ASP.Net Core

How to return pdf document from controller with ajax call in MVC app

ASP .NET MVC - return Exception message to ajax call

Passing empty list from controller to controller asp.net mvc

ajax pass a list of int to asp net api controller, parameter is null

Pass Model as a list from View to Controller using ajax call

how to use returned list in ajax call from controller

.NET Core making an AJAX call from a Razor Page to a Controller

Jquery Ajax, return success/error from mvc.net controller

Call controller function from view! asp.net MVC