rakshi rakshi - 4 months ago 18
Ajax Question

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

Answer

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