Reputation: 784
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
Upvotes: 7
Views: 3878
Reputation: 26
I would create a class that has 2 properties
int lid = seat_plane_id;
List<List<SeatPlans>> listOfSeatPlans (a collection of collections)
List<List<SeatPlans>> list = new ...
list.Add(allUser);
list.Add(someUsers);
Now you can return the class object back to JSON
Upvotes: 1
Reputation: 85
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 );
},
Upvotes: 7
Reputation: 7656
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.
Upvotes: 3