简体   繁体   English

将数组javascript作为参数传递给控制器

[英]Pass array javascript as param to controller

firstly sorry for my bad english, my problem is, i have a controller that receive a list of object as param, but when i try sent this list to controller always receive null. 首先抱歉我的英文不好,我的问题是,我有一个控制器接收对象列表作为参数,但是当我尝试将此列表发送到控制器时总是收到空。

    public async Task<ActionResult> MyAction(List<class> object)
    {

        //do stuff
    }

my javascript 我的javascript

 var array= [] array.push()... document.location = '/MyAction/Controller?object'JSON.stringify(array) my controller always receive null 

i alreay tried use ajax, but for some reason ajax call my controler twice, in first call i recieve my list correctly, but in second receive null 我似乎尝试使用ajax,但由于某种原因ajax调用我的控制器两次,在第一次调用我正确收到我的列表,但在第二次接收null

 var array =[] array.push()//just example $.ajax({ type: 'POST', tradicional:true, async:true, url: '/controller/MyAction', data: JSON.stringify({ 'object': array}), contentType: 'application/json; charset=utf-8', datatype: 'json', success: function (result) { //if succes then load my View passing array as param document.location = '/controller/MyAction?object' + JSON.stringify(array) ; }, error: function (result) { } }); 

i alreay tried use ajax, but for some reason ajax call my controler twice 我试过使用ajax,但由于某种原因,ajax调用我的控制器两次

I'm guessing the first call is a POST request done by the lines: 我猜第一次调用是由行完成的POST请求:

$.ajax({
    type: 'POST',

and the second call is a GET request done by the line: 第二个调用是由该行完成的GET请求:

  //if succes then load my View passing array as param
  document.location = '/controller/MyAction?object' + JSON.stringify(array) ;

Check if this is the case in the "network" tab of your browser's debugger, decide whether you want to use POST or GET, and configure your controller accordingly. 检查浏览器调试器的“网络”选项卡中是否存在这种情况,确定是要使用POST还是GET,并相应地配置控制器。 This thread may be helpful: asp mvc http get action with object as parameter 这个主题可能会有所帮助: asp mvc http get object with object as parameter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM