0

I have a rather peculiar problem when trying to use the jQuery getJSON function.

I try to send my parameters by making an object like so:

var args = {
    from: "",
    to: "",
    customerId: "52"
    articles: ['12312', '21521']
};

Then call the getJSON function:

$.getJSON('/Statistics/TimeGraph', args, function (response) {
    //Fill graph.
});

This is where the problem starts. I recieve the request on the controller, but articles is not populated (the other parameters are).

Controller action:

public JsonResult TimeGraph(DateTime? from, DateTime? to, int? customerId, string[] articles)
{
    return Json("", JsonRequestBehavior.AllowGet);
}

Is it not possible to send an array inside an object like this? Or is there something I'm missing?

1 Answer 1

2

You need to set traditional parameter to true, otherwise it will not work correctly.

$.getJSON('/Statistics/TimeGraph', $.param(args,true), function (response) {
    //Fill graph.
});

or simple ajax call

 $.ajax({
        type: "GET",
        url: "/Statistics/TimeGraph",
        data: args,
        success: function(response){
             //Fill graph.
        },
        dataType: "json",
        traditional: true
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, thanks, it works. Mind explaining what it means?
It's just the new way (not traditional) of how jquery serializes data is not working with asp.net mvc standard model parser. So we need to always use traditional way if we are sending arrays.

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.