I want to bind my C# datatable to the jQuery datatable. However a popup appears that says
DataTables warning: table id=extracionTable - Ajax error. For more information about this error, please see http://datatables.net/tn/7
and the table says "Loading..." but does nothing. I don't understand why this is the case.
Here is my controller.
public JsonResult loadExtraccionTable(string table_name)
{
table_name = "ext_clientes";
MetaViewContext metaViewContext = new MetaViewContext();
return Json(new { data = metaViewContext.GetMetaView(table_name).table.AsEnumerable().ToList() }, JsonRequestBehavior.AllowGet);
}
This part of the code returns a Datatable
metaViewContext.GetMetaView(table_name).table
I added the .AsEnumerable().ToList()
because I thought it would make a difference
Here is how I make my c# datatable
public DataTable simpleExportQuery(string p_table)
{
string selectStatement = "select * from " + p_table;
DataTable dt = new DataTable();
using (MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connString))
{
conn.Open();
using (MySqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = selectStatement;
using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
{
sda.Fill(dt);
}
}
}
return dt;
}
Here is my script
<script>
$(document).ready(function () {
var table = $('#extracionTable').DataTable({
"ajax": {
"url": "/extraccion/loadExtraccionTable",
"type": "GET",
"datatype": "json",
},
rowReorder: {
selector: 'td:nth-child(2)'
},
responsive: true,
dom: 'Bfrtip',
buttons: [
'copyHtml5',
'excelHtml5',
'csvHtml5',
'pdfHtml5'
],
"deferRender": true,
"columns": [
@foreach (var item in Model.lstColumns)
{
<text>
{ "data":"@(item.columnName)", "autoWidth": true },
</text>
}
],
});
});
</script>
Here is what I assume my json looks like
[
{
"cliente_id":2,
"nombre_cliente":"my name",
"direccion":"some adresss",
"cuidad":"city",
"departamento":"agricolture",
"codigo_postal":"444444",
"pais_nombre":"Colombia",
"correo_facturacion_electronica":"True",
"DANE":3,
"digito_verificacion":4444,
"NIT":3,
"plazo_pago":0,
"comercial_asignado":"some name",
"con_potencial":1,
"confirmado":1,
"eliminado":0,
"fecha_creacion":"2017-12-22T00:00:00",
"visitado":1,
"activo":1,
"comentario":"Some comment",
"contacto_id":15,
"nombre_contacto":"somename",
"apellido_contacto":"name",
"carga_contacto":"Sales",
"correo_contacto":"cliente@email.ca",
"telefono_fijo_contacto":"444444444",
"telefono_movil_contacto":"444444444",
"principal_contacto":1,
"contacto_eliminado":false
}
]
Also maybe this is useful I also found this error in chrome " A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule' "
Any help would be very appreciated thank you.