tags:

views:

49

answers:

2

I have created and filled various arrays using jquery. First time, trying to send javscript arrays to mvc controller.

Can I have an example how to do that? How can I send the arrays and other variables as well? On the controller side, how can I retrieve the data?

+1  A: 

You'll probably want to use jQuery.ajax, with a dataType parameter of 'json'. You can send any JSON object. Possible example:

var obj = {'foo': 'bar'};

$.ajax({
   type: "POST",
   url: "some.aspx",
   dataType: "json",
   contentType: "application/json; charset=utf-8", 
   data: obj,
   success: function(resp){
     alert("Response: " + resp);
   }
 });
Matthew Flaschen
A: 

You can comma or pipe delimit your input and on the other end just parse it to keep things simple. Or if you want to do things the right object oriented way you could use the following code:

var object1 = $(".ControlArrayClass").val(); 
var object2 = $(".ControlArrayClass2").val(); 
$.post('mycontroller/myactionmethod', function( variable1: object1, variable2: object2});

and on the controller end it would look like this

public ActionResult myactionmethod(Guid[] variable1, String[] variable2)
{
//do whatever here
return View(); 
}

Hope this helps.

Al Katawazi