How to send multiple jQuery arrays to an MVC contr

2019-09-21 01:56发布

问题:

I have created and filled various arrays using jquery. First time, trying to send JavaScript 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:

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.



回答2:

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);
   }
 });