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?
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
.– Jedaias Rodrigues
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.
– Luã Govinda Mendes Souza