I'm missing something here. I've got this jQuery JavaScript:

$.ajax({
    type: "POST",
    url: "/update-note-order",
    dataType: "json",
    data: {
        orderedIds: orderedIds,
        unixTimeMs: new Date().getTime()
    }
});

Where orderedIds is a JavaScript number array (e.g. var orderedIds = [1, 2]).

The handling Controller method is:

[HttpPost]
public void UpdateNoteOrder(long[] orderedIds, long unixTimeMs)
{
    ...
}

When I put a Debugger.Break() in UpdateNoteOrder(), orderedIds is null in the Watch window. (unixTimeMs, however, has a numeric value.)

How do I pass the number array through $.ajax() such that orderedIds is a long[] in my controller?

link|improve this question

50% accept rate
feedback

2 Answers

up vote 14 down vote accepted

Just set the traditional parameter to true:

$.ajax({
    type: "POST",
    url: "/update-note-order",
    dataType: "json",
    traditional: true,
    data: {
        orderedIds: orderedIds,
        unixTimeMs: new Date().getTime()
    }
});

Since jquery 1.4 this parameter exists because the mechanism to serialize objects into query parameters has changed.

link|improve this answer
feedback

you'll need to turn orderedId's into a param array, or the controller won't see it

$.param({ orderedIds: orderedIds });  

in your code:

$.ajax({
    type: "POST",
    url: "/update-note-order",
    dataType: "json",
    data: {
        orderedIds: $.param({ orderedIds: orderedIds }),
        unixTimeMs: new Date().getTime()
    }
});
link|improve this answer
1  
This does not work. In UpdateNoteOrder, orderedIds is still null. – AgileMeansDoAsLittleAsPossible Dec 9 '10 at 19:19
if it doesn't work, check your code first, this is taken directly from the jquery documentation api.jquery.com/jQuery.param You may want to be sure you are correct before rejecting an answer – Jeremy B. Dec 9 '10 at 19:22
I can confirm also that this doesn't work (I wrote a sample application in order to test it). – Darin Dimitrov Dec 9 '10 at 19:23
I tested with 1.3.2, I see now the changes were in 1.4. I guess I didn't update jquery recently. – Jeremy B. Dec 9 '10 at 19:28
in jquery 1.3.2 you don't even need $.param, it will work out of the box. Unfortunately in jquery 1.4 this behavior has changed. – Darin Dimitrov Dec 9 '10 at 19:29
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.