Good day !,
I have read some articles on DataTables.net, for example:
DataTables.net - Exemples index
Loading tables with json using DataTables
Using jquery DataTables with ASP.net MVC 5
but I can not load the data in the view:
Class ClienteX
public class ClienteX
{
public int ID { get; set; }
public string Nome { get; set; }
public int Idade { get; set; }
}
ACTION
public ActionResult Index()
{
return View();
}
public JsonResult AjaxHandler()
{
var model = new List<ClienteX>(){
new ClienteX{ID=1, Nome="João", Idade=42}
};
var Resultado = new
{
sEcho = "1",
iTotalRecords = 1,
iTotalDisplayRecords = 1,
aaData = model
};
return Json(Resultado, JsonRequestBehavior.AllowGet);
}
VIEW index.cshtml
<h2>DataTable - Ajax</h2>
<button type="button" id="btnEnviar">Enviar</button>
<table id="myDataTable" class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Idade</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script src="//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js"></script>
<link href="//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function () {
$("#btnEnviar").click(function () {
$("#myDataTable").dataTable({
"bServerSide": true,
"sAjaxSource": "DOMDataSource/AjaxHandler",
"bProcessing": true,
"aoColumns":
[
{
"sName": "ID",
"mData": "ID"
},
{
"sName": "Nome",
"mData": "Nome"
},
{
"sName": "Idade",
"mData": "Idade"
}
]
});
});
});
</script>
The push button click event works:
$("#btnEnviar").click(function () {...
But the next code that calls the JsonResult method and which should populate the table does not run, why?
$("#btnEnviar").click(function () {
$("#myDataTable").dataTable({
"bServerSide": true,
"sAjaxSource": "DOMDataSource/AjaxHandler",
"bProcessing": true,
"aoColumns":
[
{
"sName": "ID",
"mData": "ID"
},
{
"sName": "Nome",
"mData": "Nome"
},
{
"sName": "Idade",
"mData": "Idade"
}
]
});
});