0

I want to post java script object to mvc controller

$(document).ready(function () {
        var table = $('#my_table_1').DataTable({
            "paging": true,
            "ordering": true,
            "info": true,
            "search": true,
            "pageLength": 100
});
var d = '';
var data3 = table.on('search.dt', function () {
    //number of filtered rows
    //  console.log(table.rows({ filter: 'applied' }).nodes().length);
    //filtered rows data as arrays
    d = table.rows({ filter: 'applied' }).data()
});
console.log(table.rows({ filter: 'applied' }).data());
$('#excel2').click(function (e) {
    //var data3 = table.on('search.dt', function () {         
    //    console.log(table.rows({ filter: 'applied' }).data());
    //    console.log(data3);
    //});
    console.log(d);
    $.ajax({
        url: '/Administrator/TestDownload',
        type: 'POST',
        data: {data:d},
        cache: false

    }).done(function (response) {
        alert(d);
       });
    });
});

//Controller code:

public JsonResult TestDownload(String[] data)
    {
        return Json(data,JsonRequestBehavior.AllowGet);
    }

I am getting null in controller as a data parameter

Expected: Want to get data object from view to controller as a parameter in controller.

Actual: Data parameter in controller is null

1
  • 1
    Can you post console log of d variable Commented Jun 26, 2019 at 12:49

3 Answers 3

1

An example that works:

        var test = ["This", "is", "a", "test"];
        $.ajax({
            type: "POST",
            traditional: true,
            url: "Administrator/TestDownload",
            data: { array: test }
            }
        });

The controller(in VB.net):

Function TestDownload(array As String()) As ActionResult
//do something
End Function
Sign up to request clarification or add additional context in comments.

Comments

0

You must check your d variable correct array format.

I tested in my side with var d = ["test",2,3] and in controller it get correct data.

$('#excel2').click(function (e) {
    //var data3 = table.on('search.dt', function () {         
    //    console.log(table.rows({ filter: 'applied' }).data());
    //    console.log(data3);
    //});
    d = ["test",2,3]
    console.log(d);
    $.ajax({
        url: '/Administrator/TestDownload',
        type: 'POST',
        data: {data:d},
        cache: false

    }).done(function (response) {
        alert(d);
       });
    });
});

Comments

0

Why not try stringifying the data and setting the contentType

$.ajax({
    url: '/Administrator/TestDownload',
    data: JSON.stringify({data:d}), // use JSON stringify
    type: 'POST',
    contentType: "application/json; charset=utf-8", //add this
    cache: false    
}).done(function (response) {
    alert(d);
   });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.