Am trying JQUERY DataTable with MVC. First thing am trying to do is trying to set up the base.
Am trying to get the column and Datavalues Dynamically. am using fnServerData with sAjaxSource.
Am having a breakpoint in my controller file to see if the it is getting called to make sue i have set it up correctly before proceeding.
When i run this code. Am getting "TypeError: k is undefined" so controller is not getting called
When is searched for this issue closer i came is jQuery datatables issue which states
In order for DataTables to be able to function correctly, the HTML for the target table must be laid out in a well formed manner with the 'thead' and 'tbody' sections declared.
But am forming everyting dynamically so am not sure what am doing wrong. My sample code below.
Any suggestions on doing it the right way would help !
CSHMTL file
<table id="TestDatatable">
</table>
DataTable script file
$('#TestDatatable').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "Search/Testcall",
"fnServerData": function (sSource, aoData, fnCallback, oSettings) {
aoData.push({ "name": "more_data", "value": "my_value" });
oSettings.jqXHR = $.ajax({
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
});
}
});
Sample Model
public class DataTableParam
{
/// <summary>
/// Request sequence number sent by DataTable, same value must be returned in response
/// </summary>
public string sEcho { get; set; }
/// <summary>
/// Text used for filtering
/// </summary>
public string sSearch { get; set; }
/// <summary>
/// Number of records that should be shown in table
/// </summary>
public int iDisplayLength { get; set; }
/// <summary>
/// First record that should be shown(used for paging)
/// </summary>
public int iDisplayStart { get; set; }
}
Controller
public JsonResult Testcall(DataTableParam searchData)
{
return Json("", JsonRequestBehavior.AllowGet);
}
Update
Another update which i got while going through the issue is we need to set the Columns first before we assign data to DataTable. But in my scenario am trying to hit controller in an ajax call but even before that am getting the above Error.
Update
Is Dynamic DataTable not possible at all ? I ll know the Columns as well as Data only at runtime?
Thanks