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.
function()syntax of thedataoption. That allows you to add your custom data to the data provided automatically by DataTables when usingserverSide: true. See the documentation here and look at the examples"data": function ( d ) { ... }.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.