0

I know this is a popular topic and I've tried all of the solutions I could find already out there to no avail. I've used all the solutions included for this questions: Pass a List from javascript to controller. I've simplified my test to ridiculously simple. I get into the controller but my controller input param is {int[0]}. I confirmed my array data looks good in the JavaScript and ajax call.

Can anyone please tell me what I am missing?

JavaScript Code

var selectedIds = [];
selectedIds.push(565);
selectedIds.push(573);
selectedIds.push(571);
// selectedIds = [565, 573, 571]

$.ajax({
    type: "POST",
    traditional: true,
    dataType: "json",
    data: { "ids": JSON.stringify(selectedIds) },
    //data: { "ids": selectedIds},
    //data: { ids: selectedIds},
    url: "api/services/getbuildingsbyid",
    success: function (result) {
        return result;
    }
});

Controller Code

[HttpPost]
public bool GetBuildingsById(int[] ids)
{
    var lookAtIds = ids;     // {int[0]}
    return true;
}
2
  • 1
    try setting the data to { "ids": selectedIds } otherwise you're sending a string and not an int[] ex { "ids": "[565, 573, 571]"} Commented Jul 21, 2017 at 22:24
  • 2
    Alternatively (to @Enfyve comment/answer), it needs to be data: JSON.stringify({ ids: selectedIds }), with contentType: 'application/json', (and delete traditional: true,) And if your passing an array of complex objects, then this format is necessary. Commented Jul 22, 2017 at 3:38

2 Answers 2

3

By using JSON.stringify(selectedIds) you are turning an array of int into a string representation of said array.

The AJAX POST ends up like so:

{ "ids": "[565, 573, 571]" }

instead of

{ "ids": [565, 573, 571] }

If instead you just use the variable for the data, that should resolve the issue:

data: { "ids": selectedIds },

Edit-

Working backwards, by setting a variable to the data we can see the following:

var data = {"ids": JSON.stringify(selectedIds)}
typeof(data["ids"]) // string 
data["ids"][0]      // '['

data = {"ids": selectedIds}
typeof(data["ids"]) // object
data["ids"][0]      // 565
Sign up to request clarification or add additional context in comments.

1 Comment

I've tried it both ways (as shown in my question) and I tried it again this morning using your code, Using JSON.stringify(selectedIds), the typeof(data["ids"]) returns undefined. Using {"ids": selectedIds} does return an object and data["ids"][0] does return 565 but the controller still receives an empty array.
3

I use:

public ActionResult GetBuildingsById(String selectedIds)
{
    // this line convert the json to a list of your type, exactly what you want.
    IList<int> Ids = new JavaScriptSerializer().Deserialize<IList<int>>(selectedIds);     
    return Content(ids[0].ToString());
}

This will take you selectedIds = [565, 573, 571] and create the list of your type List

1 Comment

+1 for alternative method; but if you already know the format of your data, why tax the server by making it deserialize the data when you can send it in the appropriate format to begin with?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.