ASP.NET MVC Int Array parameter with empty array defaults to {0}

后端 未结 2 1608
野趣味
野趣味 2021-01-18 19:42

I have a controller action like:

Public ActionResult MyAction(int[] stuff){}

I make a JSON request like:

$.getJSON(url, { s         


        
2条回答
  •  清歌不尽
    2021-01-18 20:04

    When I tested the code I got null array in the controller action and not an array with one element.

    In jquery 1.4 and later the way parameters are serialized during an AJAX request have changed and is no longer compatible with the default model binder. You could set the traditional parameter when performing the request:

    $.getJSON(url, $.param({ stuff: [ 1, 2, 3 ] }, true));
    

    or

    $.ajax({
        url: url,
        type: 'GET',
        dataType: 'JSON',
        data: { stuff: [ 1, 2, 3 ] },
        traditional: true,
        success: function(res) { }
    });
    

提交回复
热议问题