Sending javascript array to code (C #) using ajax

I am a little new to C # and javascript, so while my question is specific, I am open to any alternatives.

I have an array of values โ€‹โ€‹(which I created in a javascript function) that I want to send a file to my code that will be used in the method. From what I explored with ajax and an array string with JSON, seems like a better method.

My questions

  • Can I pass an array using this method?

  • How to capture server side information (in my code?)

Javascript passing values

var jsonvalues = JSON.stringify(values); var callback = window.location.href $.ajax({ url: callback type: "POST", contentType: 'application/json', data: jsonvalues }); 

I saw a lot of solutions using [WebMethod] or some kind of WebService to capture data, can I use this to work in my code file without returning data?

Here is what I use in my file with code below

 [WebMethod] public static void done(string[] ids) { String[] a = ids; } 
+7
source share
3 answers

I wrote a detailed example of this using ASP.NET MVC, but it can be easily adapted for WebForms.

Send data using jquery to MVC controller

HTML and jQuery will look almost exactly the same, except where you call WebMethod.

If the page you are using is called Default.aspx and the method is called Done , then your URL for WebMethod will be Default.aspx/Done .

 <script> // Grab the information var values = {"1,","2","3"}; var theIds = JSON.stringify(values); // Make the ajax call $.ajax({ type: "POST", url: "Default.aspx/Done", // the method we are calling contentType: "application/json; charset=utf-8", data: {ids: theIds }, dataType: "json", success: function (result) { alert('Yay! It worked!'); }, error: function (result) { alert('Oh no :('); } }); </script> 

Your WebMethod will still match.

 [WebMethod] public static void done(string[] ids) { String[] a = ids; // Do whatever processing you want // However, you cannot access server controls // in a static web method. } 
+4
source

The easiest way is to use ASP.NET MVC and bind the data to a list. Therefore, for a list of strings this would be very simple. Just do a controller action that looks like this:

 [HttpPost] public ActionResult MyAction(string[] values) { ... debug and see that values gets set to your array from javascript ... } 

and then pass data: values in your call to $.ajax . No need to tidy up, jQuery will determine what to do. For more complex bundles of lists, check this out (and many other resources, for example, talk about fantastic ways to bind to complex lists of objects):

http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

To call [WebMethod] methods from web pages or web services, check out this guide:

http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Basically, although you need a ServicePage.aspx/MethodName

0
source

Put your data in a hidden field using the runat = server. Submit the form and retrieve the data as usual.

-one
source

All Articles