Submit array param with jQuery ajax/load

后端 未结 4 1788
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-13 10:05

public ActionResult DoSomething(string[] arr, bool someBool, int someInt) { }

trying to call the above method from jQuery:

var test = [];
te         


        
4条回答
  •  忘了有多久
    2021-02-13 10:26

    If you are using jquery 1.4 you might need to set the traditional parameter to true in order to be compatible with the default model binder format in ASP.NET MVC:

    var test = [];
    test.push('dog');
    test.push('cat');
    
    $.ajax({
        url: 'MyController/DoSomething',
        type: 'GET',
        traditional: true,
        data: { arr: test, someBool: true, someInt: 1 },
        success: function(result) {
            $container.html(result);
        }
    });
    

    or if you prefer the .load() method:

    var data = { arr: test, someBool: true, someInt: 1 };
    $container.load('MyController/DoSomething', $.param(data, true), 
        function(response, status, xhr) {
        // ...
    });
    

提交回复
热议问题