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
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.
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? 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); } }).
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.
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 );
},
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With