Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC model binding a string array from jQuery post

The parameter string[] orderTypeNames is coming up null.

mvc action

public PartialViewResult EditMultipleOrderStates(
    string[] orderTypeNames,
    int[] orderIds)

javascript

$('#edit-mulitple-order-states-button').click(function () {
    ids = [];
    types = [];
    $checked = $('.order-queue-order input:checked');
    $orders = $checked.closest('.order-queue-order');
    $orders.each(function (index, elem) {
        $order = $(elem);
        ids.push($order.attr("orderId"));
        types.push($order.attr("orderType"));
    });
    data = {
        orderIds: ids,
        orderTypeNames: types
    };
    $.post('EditMultipleOrderStates', data, function (response) {
        //...
    });
});

fiddler

enter image description here

orderIds%5B%5D=97&orderIds%5B%5D=98&orderIds%5B%5D=93&orderTypeNames%5B%5D=DeliveryOrder&orderTypeNames%5B%5D=DeliveryOrder&orderTypeNames%5B%5D=DeliveryOrder

Is it the square brackets causing the problem? How can I bind to these arrays?

Edit: I am manually building the query string in the mean time.

query = "";
for each...
query += "orderIds=" + $order.attr("orderId") + "&";
query += "orderTypeNames=" + $order.attr("orderType") + "&";
like image 578
Benjamin Avatar asked Jul 20 '12 15:07

Benjamin


1 Answers

You need to call some sort of JSON.stringify on data.

 $.post('EditMultipleOrderStates', JSON.stringify(data), function (response) {
        //...
    });
like image 178
scottm Avatar answered Oct 07 '22 18:10

scottm