Passing .NET objects to method with AJAX

Clash Royale CLAN TAG#URR8PPPPassing .NET objects to method with AJAX
I'm calling a method in my C# code that takes 2 ListBox objects and a GridView as parameters. How do I pass these, what I've tried throws an error saying the parameters are missing.
var visList = document.getElementById("<%= shown_ListBox.ClientID%>");
var hidList = document.getElementById("<%= hidden_ListBox.ClientID%>");
var gv = document.getElementById("<%= hm_GridView.ClientID%>")
$.ajax({
type: "POST",
url: "heatmap.aspx/manageCol",
data: JSON.stringify({visList, hidList, gv}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (res) {
alert("success");
},
error: function (res, msg, code) {
// log the error to the console
} //error
});
public static void manageCol(ListBox visList, ListBox hidList, GridView gv)
{
//Stuff
}
stringify({shown, hidden, gv}
I've matched the names and an error is no longer thrown but all the objects have no data once they are passed. And I'm sure it is, this is new stuff to me. I could post the list data if that would make things easier but the gridview is bound to an sql data source so I would need to keep that.
– Mendi16
54 mins ago
I had a feeling that may happen: I have never seen anyone, including myself, post a control to the server. I suggest you study how to work with gridviews and get a hang of it. You can apply that knowledge to the other controls too.
– CodingYoshi
50 mins ago
Posting the gridview isn't critical, I could work around that. How can I pass the listbox data to the method?
– Mendi16
40 mins ago
Read about M in MVC. You will pass a model to the controller. In other words, you will get the data from the listbox, create an object from it, same for gridview and then pass the whole object to the controller.
– CodingYoshi
36 mins ago
1 Answer
1
1) JSON.stringify({visList, hidList, gv} is sending one object to your webmethod manageCol while it's expecting 3 parameters.
2) you are passing one string object while expecting ListBox, ListBox and GridView items.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Make sure the names match to the controller params:
stringify({shown, hidden, gv}. Also, it is very odd to post lists and gridviews becaise they will have so much html; normally you would post the data which is being manipulated in those controls. For example, you may have a gridview for manipulating student records and you will post only those student records which have been changed.– CodingYoshi
1 hour ago