Working with ASP.NET MVC, I am using the library DataTable.net
I am bringing data from the database through a JsonResult that returns the information in plain text.
Method
// GET: Cliente
public ActionResult Index()
{
List<Cliente> _cliente = clienteService.GetAll().ToList();
config = new MapperConfiguration(cfg => cfg.CreateMap<Cliente, ClienteViewModel>());
List<ClienteViewModel> list = config.CreateMapper().Map<List<ClienteViewModel>>(_cliente);
return Json(list, JsonRequestBehavior.AllowGet);
}
From the HTML
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#clientes").DataTable({
paging: true,
searching: true,
bProcessing: true,
"ajax": {
"url": "Cliente/Index",
"columns": [
{ "data": "ClienteId" },
{ "data": "RazonSocial" },
{ "data": "NumeroDocumento" },
{ "data": "Direccion" },
{ "data": "Fijo" },
{ "data": "Email" },
{ "data": "Estado" }
],
"dataType": "json"
},
"language": {
"sProcessing": "Procesando...",
"sLengthMenu": "Mostrar _MENU_ registros",
"sZeroRecords": "No se encontraron resultados",
"sEmptyTable": "Ningún dato disponible en esta tabla",
"sInfo": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
"sInfoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
"sInfoFiltered": "(filtrado de un total de _MAX_ registros)",
"sInfoPostFix": "",
"sSearch": "Buscar:",
"sUrl": "",
"sInfoThousands": ",",
"sLoadingRecords": "Cargando...",
"oPaginate": {
"sFirst": "Primero",
"sLast": "Último",
"sNext": "Siguiente",
"sPrevious": "Anterior"
},
"oAria": {
"sSortAscending": ": Activar para ordenar la columna de manera ascendente",
"sSortDescending": ": Activar para ordenar la columna de manera descendente"
}
}
});
});
The problem is that it shows me a page with the following information
I think the issue is to leave the Index as ActionResult and create a JsonResult method that loads the table.
Updating how I have my methods. Controller
// GET: Cliente
public ActionResult Index()
{
return View();
}
public JsonResult ListaClientes()
{
List<Cliente> _cliente = clienteService.GetAll().ToList();
config = new MapperConfiguration(cfg => cfg.CreateMap<Cliente, ClienteViewModel>());
List<ClienteViewModel> list = config.CreateMapper().Map<List<ClienteViewModel>>(_cliente);
return Json(list, JsonRequestBehavior.AllowGet);
}
HTML
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#clientes").DataTable({
//paging: true,
//searching: true,
//bProcessing: true,
"ajax": {
"url": "@Url.Action("ListaClientes")",
"dataSrc": '',
"type": "GET",
"columns": [
{ "data": "ClienteId" },
{ "data": "RazonSocial" },
{ "data": "NumeroDocumento" },
{ "data": "Direccion" },
{ "data": "Fijo" },
{ "data": "Email" },
{ "data": "Estado" }
],
//"dataType": "json"
},
"language": {
"sProcessing": "Procesando...",
"sLengthMenu": "Mostrar _MENU_ registros",
"sZeroRecords": "No se encontraron resultados",
"sEmptyTable": "Ningún dato disponible en esta tabla",
"sInfo": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
"sInfoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
"sInfoFiltered": "(filtrado de un total de _MAX_ registros)",
"sInfoPostFix": "",
"sSearch": "Buscar:",
"sUrl": "",
"sInfoThousands": ",",
"sLoadingRecords": "Cargando...",
"oPaginate": {
"sFirst": "Primero",
"sLast": "Último",
"sNext": "Siguiente",
"sPrevious": "Anterior"
},
"oAria": {
"sSortAscending": ": Activar para ordenar la columna de manera ascendente",
"sSortDescending": ": Activar para ordenar la columna de manera descendente"
}
}
});
});
Error DataTables warning: table id = clients - Requested unknown parameter '0' for row 0, column 0. For more information about this error, please see