Send Javascript Array to Controller via POST

Asked

Viewed 2,741 times

2

I’m trying to send a Array of int of JavaScript to the Controller via POST.
See my array:

console.debug(itens);

Array [ 1, 2 ]

I’m trying to send it this way:

$.post('@Url.Action("MinhaAction", "MeuController")', { MeuParametro : itens })

Meucontroller is like this:

[HttpPost]
public ActionResult MinhaAction(int[] MeuParametro)

But when debugging, I have received null in my controller, because instead of sending:

MeuParametro : [1,2]

Javascript has been sending:

MeuParametro[] : "1"
MeuParametro[] : "2"

I tried to use JSON.parse():

 $.post('@Url.Action("MinhaAction", "MeuController")', { MeuParametro : JSON.parse(itens) })

And it has worked only when I have only one item in my array, but when I have two or more (as is my case), the following error is returned:

Syntaxerror: JSON.parse: Unexpected non-whitespace Character after JSON data at line 1 column 3 of the JSON data jquery-1.8.2.js line 578 > Val:126:71

Could someone help me with this?

3 answers

1

Good morning, Grande also tries to create a pattern in your items variable, more or less like this, items.id, so when sending to the controller he will identify a json:

itens:{
    {"id":"1"},
    {"id":"2"}
}

1


It worked by following the hint of these answers:

Using:

jQuery.ajaxSettings.traditional = true;

The problem is that the settings are global. If you want something different in another post, this will cause problems.
So I could use it normally:

$.post('@Url.Action("MinhaAction", "MeuController")', { MeuParametro : itens })

1

You can do it this way :

var countryArray = [1,2,3,4];

    $.ajax({
        type: "POST",
        url: "../Home/SaveTable",
        contentType: 'application/json',
        data: JSON.stringify({ MeuParametro : countryArray}),
    });

I mean, instead of doing JSON.parse(array) you will have to do JSON.stringify({ MeuParametro : countryArray}

Difference between JSON.parse and JSON.stringify

According to Ben Smith’s 'Basic Json' Book "there is a stringify method that generates JASON serialized from a given.". Soon after informs that there is a method that is the opposite of stringify "This method is known as parse'. In a nutshell, JSON.parce converts serialized JSON to Javascript values"

Book link

Important links :

https://stackoverflow.com/questions/17785592/difference-between-json-stringify-and-json-parse

https://stackoverflow.com/questions/21288462/c-sharp-mvc-4-passing-javascript-array-in-view-to-controller

https://stackoverflow.com/questions/5497093/what-is-traditional-style-of-param-serialization-in-jquery/5497151#5497151

  • Well friend, thank you very much for the answer, but the question is that I would like to use the method $.post instead of $.ajax.

  • In fact the $.post method is derived from $.ajax. but with some simplifications. I will check the possibility of using the $.post and post a new answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.