Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing array from javascript to controller MVC 4

I am using razor and I'm having a hard time passing an array to a controller. the array contains objects that I made and I am trying to do this:

$.ajax({
     type: "POST",
     url: "HomePage/HandleOperations",
     data: JSON.stringify(operationCollection),
     success: function (data) { alert("SUCESS");},
     dataType: "json",
     contentType: "application/json"
});

and my controller is:

 public void HandleOperations(List<string> operationCollection)
 {

 }

I am not required to use ajax but I am not sure how else it could be done. In the controller it shows that the "operationCollection" contains elements but they are all null.

like image 571
laitha0 Avatar asked Feb 26 '26 07:02

laitha0


2 Answers

the Ajax parameter

traditional : true

will do the trick.

like image 81
Nalan Madheswaran Avatar answered Feb 28 '26 21:02

Nalan Madheswaran


Usage of the traditional: true parameter for an ajax call:

To help radbyx, using the "traditional: true" property of an ajax call, like the following, will tell ajax to use the traditional form of serialization. More details: http://api.jquery.com/jQuery.param/ or What is "traditional style of param serialization' in JQuery.

$.ajax({
     type: "POST",
     url: "HomePage/HandleOperations",
     data: {operations: operationCollection},
     traditional: true,
     success: function (data) { alert("SUCCESS"); }
});
like image 38
puddinman13 Avatar answered Feb 28 '26 20:02

puddinman13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!