1110
1110

Reputation: 6829

Issue with sending array to action method

I want to send array to my action method:

var items = { 'myIdList[]': [] };

        $(':checkbox').change(function () {            
            $(":checked").each(function () {
                items['myIdList[]'].push($(this).val());
            });
            $('#locationsCheckList').submit();
        });

        $('#locationsCheckList').submit(function () {
            $.ajax({
                url: this.action,
                type: this.method,
                traditional: true,
                data: { "myIdList": items }...

Action method:

[HttpPost]
        public void GetLocations(int[] myIdList)...

items variable have data but when I pass it like this I get null but if I change

data: { "myIdList": items }

with

data: { "myIdList": [1,2,3,4,5] }

it works. When I debug in browser in items variable I have values:

0: "1"
1: "2"
2: "3"

I can't pass array and I don't know why, if it works hardcoded?

Upvotes: 0

Views: 175

Answers (2)

Dan A.
Dan A.

Reputation: 2924

What if you use a simple array, similar to your example that works:

var items = [];
// your jQuery loop
items.push($(this).val());
// and so on
data: { "myIdList": items }...

Upvotes: 1

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114347

Your AJAX call needs to include:

dataType: "json",

Upvotes: 0

Related Questions