25

How can I send int array from $.ajax to c# mvc?

5
  • I tried 3-4 examples I can't run it. What can be a reason? Commented Feb 2, 2012 at 9:28
  • [Ajax] public bool example(int[] ints) Commented Feb 2, 2012 at 9:28
  • 1
    please give more detail of what you have tried and what result you are seeing, including code. Commented Feb 2, 2012 at 9:47
  • I want to check all examples, but most of them send null to c# method. Commented Feb 2, 2012 at 10:01
  • I think @Darin Dimitrov has given the appropriate answer here for this Commented May 26, 2016 at 10:25

5 Answers 5

41
$.ajax({
          url: <Url of the action>,
          type: "POST",
          data: JSON.stringify([1,2,3]),
          dataType: "json",
          contentType: 'application/json; charset=utf-8'
});

and in the action.

public ActionResult ReceiveIntArray(int[] ints)
{
   ...
}

mvc should parse the json automatically.

check out this question.

4
  • 2
    Doesn't work from my code... can't catch the reason. When I enter the method parameter is null. Commented Feb 2, 2012 at 9:29
  • 3
    sorry you would have to use JSON.stringify([1,2,3]) i have edited the answer
    – Daniel
    Commented Feb 2, 2012 at 9:44
  • @Daniel it works for me too but, i have one question. why didn't matter what array name is ("ints")? Normally if data: { BasvuruId: BasvuruId}, in js , Variable name must be same (BasvuruId) in action.
    – MustafaP
    Commented Jan 9, 2014 at 13:47
  • dataType:"json" is often overlooked... at least, by me.
    – DavidCC
    Commented Aug 10, 2019 at 19:57
3

Try solution from this question:

Set the traditional property to true before making the get call. i.e.:

jQuery.ajaxSettings.traditional = true

$.get('/controller/MyAction', 
    { vals: arrayOfValues }, 
    function (data) {
      ...
    }
1
  • Code doesn't send values. Link is nice. Commented Feb 2, 2012 at 10:04
2

The way I'm doing it is with a simple input:hidden element

<input type="hidden" name="elements" value='@String.Join(",", ViewBag.MyArray)' />

And in the JavaScript code I'm passing it as a string:

$.ajax({
   type: "POST",
   url: '/Controller/Method',
   data:
      {
          recipients: $("input[name=elements]").val()
      },
      traditional: true,
      success: updateSelected
});

And finally I just Split the elements like this:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Method(string elements)
{
    IList<long> selected = elements.Split<long>(',');
    ...
}
0
2

The simplest way would be to send a delimited (commas, possibly) string list of the ints as an argument on a GET request, then use Sting.Split() to parse them on your C# MVC receiver.

So, for example $.get("/path-to/receiver/", { myArray: myArray.toString() } );

Then, on the server, use

string[] stringArray = Request.QueryString["myArray"].ToString().Split(',')

to extract the values to a string array, then Int32.TryParse to finally get an array of ints.

jQuery GET Syntax
JS Array toString() syntax

2
  • Doesn't send values. There is nothing to parse. Commented Feb 2, 2012 at 10:08
  • This is just an example - you would need to replace "myArray" etc with your own values. The request url should be something like /path-to/receiver/?myArray=your,array,contents Commented Feb 2, 2012 at 10:29
0

Try this solution :

var Array = [10, 20, 30];

$.ajax({
    type: "Post",
    datatype: "Json",
    url: <Url of the action>,
    data: JSON.stringify(Array),
    contentType: 'application/json; charset=utf-8',

});

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.