In ASP.NET, I have collected a list of selected items from a checkbox in an array at the client side. Now I need to pass the array from client to server's ASMX web method. How do I do this?
-
Is it an ASMX Web Service you're using on the backend? Are you using any JavaScript libraries?Greg B– Greg B2011-09-29 07:32:55 +00:00Commented Sep 29, 2011 at 7:32
-
yes,using ASMX web service & jquerywebminal.org– webminal.org2011-09-29 07:34:18 +00:00Commented Sep 29, 2011 at 7:34
-
what is the question? is it a trouble of converting a List to an Array (solution: List<T>.ToArray() ) or what?Emanuele Greco– Emanuele Greco2011-09-29 07:34:21 +00:00Commented Sep 29, 2011 at 7:34
-
1The problem is, i have the contents in an array at client side,I need to pass that array to web method and convert that array into list on the server. But i have trouble in passing array from client to server side's web method.webminal.org– webminal.org2011-09-29 07:38:11 +00:00Commented Sep 29, 2011 at 7:38
-
1possible duplicate of how do you pass an array string[] to a Web Service via Jquery?Greg B– Greg B2011-09-29 08:03:44 +00:00Commented Sep 29, 2011 at 8:03
|
Show 1 more comment
2 Answers
Add a JSON library to your page, use json2.js here. That gives you a function to serialize javascript arrays into JSON strings.
You can then pass it into your webmethod:
[WebMethod]
public void MyWebMethod(List<string> someValues)
{
// Use someValues...
}
Here's the javascript you need
var arrayData = ["1","2","3"]; // Your array goes here
$.ajax({
type: "POST",
url: "MyWebService.asmx/MyWebMethod",
data: JSON.stringify({ someValues: arrayData }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function()
{
// Your success function...
}
});