Ajax 视图服务器端处理中的 ASP.Net C# 控制器返回数据表

问题描述 投票:0回答:0

我试图控制从 ajax 返回的数据表,但出现错误:

DataTables warning: table id=tbl_model - Invalid JSON response. 

Full error text window.
有关此错误的更多信息,请参阅

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;
 }

控制器返回 enter image description here

通过服务器端处理返回数据表。

c# asp.net ajax asp.net-mvc datatable
© www.soinside.com 2019 - 2024. All rights reserved.