我试图控制从 ajax 返回的数据表,但出现错误:
DataTables warning: table id=tbl_model - Invalid JSON response.
datatables.net
datatables - 1. Warning: Invalid JSON response下面是我的源代码
查看
<thead>
<tr>
<th class="text-center"></th>
<th class="text-center"><b>User</b></th>
<th class="text-center"><b>Activity</b></th>
<th class="text-center"><b>Date</b></th>
</tr>
</thead>
</table>
<script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function () {
$("#tbl_model").DataTable({
"processing": true,
"serverSide": true,
"filter": true,
"ajax": {
"url": "@Url.Action("UserAuditTrail", "AuditTrail")",
"type": "POST",
"datatype": "json"
},
"columnDefs": [{
targets: 0,
render: function (data, type, row, meta) {
return meta.row + meta.settings._iDisplayStart + 1;
}
}],
"columns": [
{ "data": "fld_ID", "name": "Id", "autoWidth": true },
{ "data": "fld_CreatedBy", "name": "User", "autoWidth": true },
{ "data": "fld_UserActivity", "name": "Activity", "autoWidth": true },
{ "data": "fld_CreatedDT", "name": "Date", "autoWidth": true },
]
});
});
</script>
控制器
public DataTable UserAuditTrail()
{
int currentYear = DateTime.Now.Year;
var getdata = db.tblUserAuditTrails.Where(x => x.fld_CreatedDT.Value.Year == currentYear).OrderBy(o => o.fld_CreatedDT).ToList();
string json = JsonConvert.SerializeObject(getdata);
DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));
return dt;
}
通过服务器端处理返回数据表。