0

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?

1
  • do you use fiddler2? Its really good for these situations, you will see exactly the difference between those two situations. Not to mention it makes ajax programming a whole lot easier... fiddler2.com/fiddler2/version.asp Commented Jan 24, 2012 at 21:56

2 Answers 2

1

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 }...
Sign up to request clarification or add additional context in comments.

Comments

0

Your AJAX call needs to include:

dataType: "json",

2 Comments

Use Firefox + Firebug, open the NET tab. You can see the details of your request (see if your data is there) as well as the response.
Now I see wired thing. In firebug I see that 'items' has values (item 0 value 5 etc.) but I have tried to call 'items.length' and I get undefined???

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.