0

I am using datatables for fetcing the data from the controller but the issue is that i have to pass some additional parameter for getting the record from the db but the issue is that when i use the sAjaxSource in the datatable request it automatically send all the parameter with the requst like pagination size , columns and sorting and other details without even mentioning it anywhere but when i use the explict ajax request like this.

    ajax: {
        type: "POST",
        data: {accountID : "45d4f5d4f5d"}
    }

it only send this parameter which is mentioned in the data and doesn't send other parameter which get sent by default when the request is made using sAjaxSource.

when i send the request like this it works:

       $(function () {
            var tableTemplates = $('#Voicemailtable').DataTable({

                sAjaxSource: "@Url.Action("VoicemailList", "Voicemail")",
                serverSide: true,
                bServerSide: true,
                bProcessing: true,
                processing: true,
                columns: [
                    {
                        data: null, // <-- This is index column
                        sortable: false,
                        autoWidth: false
                    },
                    {
                        data: "context",
                        sortable: false,
                        autoWidth: false


                    },
                    {
                        data: "created",
                        render: function (data) {
                            return moment(data).format('MM/DD/YYYY hh:mm:ss');
                        }
                    }
                    ,
                    {
                        data: "guid",
                        sortable: false,
                        autoWidth: false
                    }
                    ,
                    {
                        data: "mailbox",
                        sortable: false,
                        autoWidth: false
                    }
                    ,
                    {
                        data: "password",
                        sortable: false,
                        autoWidth: false
                    }
                    ,
                    {
                        data: "uniqueid",
                        sortable: false,
                        autoWidth: false
                    }
                ]
            });
            tableTemplates.on('draw.dt',
                function () {
                    var PageInfo = $('#Voicemailtable').DataTable().page.info();
                    tableTemplates.column(0, { page: 'current' }).nodes().each(function (cell, i) {
                        cell.innerHTML = i + 1 + PageInfo.start;
                    });
                });

        });
    public ActionResult VoicemailList(JqueryDatatableRequestModel model,string accountId)
         {
            var totalRecords = 0;
            var voicemails = voicemailService.GetVoicemailsListByAccountId(accountId, name: model.sSearch, totalCount: out totalRecords, recordStart: model.iDisplayStart, pageSize: model.iDisplayLength);
            var finalResults = Mapper.Map<List<VoicemailModel>>(Voicemails);
            return Json(new
            {
                model.sEcho,
                iTotalRecords = totalRecords,
                iTotalDisplayRecords = totalRecords,
                aaData = finalResults
            }, JsonRequestBehavior.AllowGet);
        }`

but when i try to add the additonal parameter it doesnt work

      $(function () {

            var table = $('#Voicemailtable').DataTable();
            var sEcho = table.settings()[0].oAjaxData.sEcho;
            var sSearch = table.search();
            var iDisplayLength = table.page.len();
            var iDisplayStart = table.page.info().start;
            var iSortCol_0 = table.order()[0][0];
            var sSortDir_0 = table.order()[0][1];

            var tableTemplates = $('#Voicemailtable').DataTable({

                sAjaxSource: "@Url.Action("VoicemailList", "Voicemail")",
                serverSide: true,
                bServerSide: true,
                bProcessing: true,
                processing: true,
                ajax: {
                    url: '@Url.Action("VoicemailList", "Voicemail")',
                    type: "Get",
                    data: {
                        accountId: "45454-df454-f4d65f4d54f"
                        sEcho: sEcho,
                        sSearch: sSearch,
                        iDisplayLength: iDisplayLength,
                        iDisplayStart: iDisplayStart,
                        iSortCol_0: iSortCol_0,
                        sSortDir_0: sSortDir_0
                    },
                },
                columns: [
                    {
                        data: null, // <-- This is index column
                        sortable: false,
                        autoWidth: false
                    },
                    {
                        data: "context",
                        sortable: false,
                        autoWidth: false


                    },
                    {
                        data: "created",
                        render: function (data) {
                            return moment(data).format('MM/DD/YYYY hh:mm:ss');
                        }
                    }
                    ,
                    {
                        data: "guid",
                        sortable: false,
                        autoWidth: false
                    }
                    ,
                    {
                        data: "mailbox",
                        sortable: false,
                        autoWidth: false
                    }
                    ,
                    {
                        data: "password",
                        sortable: false,
                        autoWidth: false
                    }
                    ,
                    {
                        data: "uniqueid",
                        sortable: false,
                        autoWidth: false
                    }
                ]
            });
            tableTemplates.on('draw.dt',
                function () {
                    var PageInfo = $('#Voicemailtable').DataTable().page.info();
                    tableTemplates.column(0, { page: 'current' }).nodes().each(function (cell, i) {
                        cell.innerHTML = i + 1 + PageInfo.start;
                    });
                });

        });

I tried making request from different pattern but its not working if someone could guide my it would be really appriciated.

10
  • Use the function() syntax of the data option. That allows you to add your custom data to the data provided automatically by DataTables when using serverSide: true. See the documentation here and look at the examples "data": function ( d ) { ... }. Commented Jan 30, 2023 at 13:38
  • Does this answer your question? Pass data parameter using AJAX to PHP for server side processing in datatables Commented Jan 30, 2023 at 13:43
  • When i use the ajax: { "url": "@Url.Action("VoicemailList", "Voicemail")", "data": function (d) { d.accountId = 2015; } }, it only passes the mentioned parameter and all the default values like page number and etc don't get passed they appear as null in the controller Commented Jan 31, 2023 at 13:51
  • If you add console.log(d); to the function as its final statement, what do you see in the browser console? I see all of the server-side parameters (draw, start, length...) with their expected non-null values, plus your new parameter: accountId: 2015 - also as expected. If that is not what you see, then I think there is some other problem, not shown in your question. Commented Jan 31, 2023 at 14:05
  • Just as a side-note, I recommend using POST not GET for this type of Ajax call. Send the data in the request body, not as a query string. This is not related to your problem - just an observation/suggestion. Commented Jan 31, 2023 at 14:07

0

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.