Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to return the multiple lists from controller action to ajax success call back function

I am creating a mvc .net project in which i have the jquery ajax request is as follows

$.ajax({
        url: "@Url.Action("getdata", "SeatPlans")",
        data: { seat_plane_id : 17},
    type: "POST",
    dataType: "json",
    success: function (data) {
        loadData(data);
    },
    error: function () {
        alert("Failed! Please try again.");
    }
});

which call the following controller action

public JsonResult getdata(int seat_plane_id)
    {
        int lid = seat_plane_id;
        List<SeatPlans> allUser = new List<SeatPlans>();
        allUser = db.SEATPLAN.Where(d => d.layout_id == lid).ToList();
        lid++;
        List<SeatPlans> allUser1 = new List<SeatPlans>();
        allUser1 = db.SEATPLAN.Where(d => d.layout_id == lid).ToList();

        return new JsonResult { Data = allUser,JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }

the code is working fine. the controller action send the data in allUser to callback function.

but what i need is that i want to send both data in alluser and allUser1 to the success function of ajax call

like image 837
rakshithrai Avatar asked May 13 '16 09:05

rakshithrai


People also ask

How does ajax return success data?

The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds. The function takes three parameters, namely: The data returned from the server, formatted according to the dataType parameter, or the dataFilter callback function.

How do you call a controller in a success with ajax?

So, add a success option to your partial-view ajax request and then get the response and render it in the div you want the partial-view to be visible as: success: function(response){if(response!= null) $('<yourDivInWhich you want to render the partial view>'). html(response)} .

How send multiple variables ajax?

How send multiple variables Ajax? send multiple data using ajax $. ajax({ url: "/pakainfo_api", type: "GET", data: {p1: "value1", p2: "value2"}, // multiple data we want to send success: function(data){ console. log(data); } }).

What is the return type of ajax call?

The server should return valid JavaScript that passes the JSON response into the callback function. $.ajax() will execute the returned JavaScript, calling the JSONP callback function, before passing the JSON object contained in the response to the $.ajax() success handler.


2 Answers

I imagine you want to keep the lists separated. Wrap them in an object.

var data = new { allUser = allUser , allUser1 = allUser1 }; 
return Json(yourObject, JsonRequestBehavior.AllowGet);

You can access them in your JS like this:

success: function (data) {
   var allUser = data[0];
   var allUser1 = data[1];
   //use the data as you see fit.
   loadData(allUser);
   loadData(allUser1 );
},
like image 107
Codrin Faca Avatar answered Oct 21 '22 06:10

Codrin Faca


You just have to modify your Where clause so you don't need two different lists for the users. Try this in your getdata method:

public JsonResult getdata(int seat_plane_id)
{
    int lid = seat_plane_id;
    List<SeatPlans> allUser = new List<SeatPlans>();
    allUser = db.SEATPLAN.Where(d => d.layout_id == lid || d.layout_id == (lid+1)).ToList();

    return new JsonResult { Data = allUser,JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}

allUser now includes all the desired data.

like image 29
diiN__________ Avatar answered Oct 21 '22 06:10

diiN__________