简体   繁体   中英

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

 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

 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

I'm guessing the first call is a POST request done by the lines:

$.ajax({
    type: 'POST',

and the second call is a GET request done by the line:

  //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. This thread may be helpful: asp mvc http get action with object as parameter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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