0

I am working on a scenario where, need to pass datatable Data to server side. Here is what i tried -

To get Datatable data in view -

var oTable = $('#DetailTable').dataTable();
var data = oTable.fnGetData();

Then trying to post data using ajax cal -

$.ajax({
    type: "POST",                   
    dataType: 'text',
    data:  data,
    url: "/Admin/SaveAll",                  
    success: function (result) {
    }
});

And in Controller action method -

public ActionResult SaveAll(string data)
{ 
}

I am getting null value at server side. How to pass all datatable data to server side ?

Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
user2423609
  • 33
  • 1
  • 2
  • 8
  • Do you have `dom` element(s) in you data table ? – Aria Oct 25 '16 at 14:03
  • Why you want to pass all datatable data to server ? I think you scenario is not good. – Aria Oct 25 '16 at 14:09
  • Yes, i have one image "delete" in each row. – user2423609 Oct 25 '16 at 14:13
  • In this scenario, user allowed to create a Master record with number of child records. So as user adds child record against Master record, child records are showed in datatable then on submit click what to take all records to Server side. – user2423609 Oct 25 '16 at 14:15
  • So from what I have understood, why not send child records with theirs parent Id to server instead of all Master and Details records ? – Aria Oct 25 '16 at 14:20
  • Yes you are right. I want to send only the child records which are in datatable. – user2423609 Oct 25 '16 at 14:35
  • well, can you retrieve child records of master record? – Aria Oct 25 '16 at 14:45

2 Answers2

0

Specifically in this case you are using the old style API ( fnGetData ) with a new style API object. I would suggest you use row().data() instead.

0

The data parameter in you SaveAll method is string in the other side you are getting table record by fnGetData() this function will return record(s) in JSON format so you should to make string of JSON and then pass it as Ajax data,

so try this

var postData = JSON.stringify({ 'data': data});

    $.ajax({
        type: "POST", 
        data: postData,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: "/Admin/SaveAll",                  
        success: function (result) {
        }
    });

as you can see this link there was discussed how to send datatable data to server by ajax and they are using JSON.Stringify.

if you get circular refrence error while stringifying datatable data please take a look at here and here there are some good advice and you may find external lib to do this.

I hope this give you a hand to resolve your problem.

Aria
  • 3,313
  • 1
  • 16
  • 46
  • I am getting error "0x800a13aa - JavaScript runtime error: Circular reference in value argument not supported" – user2423609 Oct 25 '16 at 13:43
  • I have tested it and `postData` initiated by data table string data, did you try clearly? – Aria Oct 25 '16 at 14:01
  • var postData line throwing same error - 0x800a13aa - JavaScript runtime error: Circular reference in value argument not supported – user2423609 Oct 25 '16 at 14:11