Denys Wessels
Denys Wessels

Reputation: 17019

Pass through an array as a $ajax parameter MVC

I have a method in my controller which takes an array as a parameter:

public JsonResult AddItemToBasketFlavours(int skuID, string description, long[] qualifiers){}

How can I pass through an array of two items to this method from javascript?

I've tried this:

$.ajax({
    url: url,
    cache: false,
    type: 'GET',
    contentType: 'application/json; charset=utf-8',
    data: { skuID: sku, description: productDescription, qualifiers: sweetSKUID,qualifiers:drinkSKUID },
    success: function (data) {

    },
    error: function (result) {

    }

});

When I step through the code in the controller the qualifiers array has only one item, I need both of them to be present

Upvotes: 1

Views: 208

Answers (1)

Steve
Steve

Reputation: 20469

You need to set the parameter traditional to true, and use a js array:

$.ajax({
    url: url,
    cache: false,
    type: 'GET',
    traditional: true, //<- set this
    contentType: 'application/json; charset=utf-8',
    data: { skuID: sku, description: productDescription, qualifiers: [sweetSKUID, drinkSKUID] },
    success: function (data) {

    },
    error: function (result) {

    }

});

Upvotes: 3

Related Questions