I'm trying to send Array
of int
from JavaScript
to Controller
via POST
.
See my array:
console.debug(itens);
Array [ 1, 2 ]
I'm trying to send it as follows:
$.post('@Url.Action("MinhaAction", "MeuController")', { MeuParametro : itens })
MyController looks 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 sent:
MeuParametro[] : "1"
MeuParametro[] : "2"
I tried using JSON.parse () :
$.post('@Url.Action("MinhaAction", "MeuController")', { MeuParametro : JSON.parse(itens) })
And it has only worked 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 > eval: 126: 71
Could anyone help me with this?