【问题标题】:Getting a Jquery DataTable Error (Unknown Parameter)获取 Jquery DataTable 错误(未知参数)
【发布时间】:2021-07-29 12:44:04
【问题描述】:

我收到以下错误。我知道有类似的已回答问题,但我已尝试使用这些答案,但仍然看不到我的错误。

错误信息:

我在表格和请求中有正确​​数量的列。我检查了错字。我正在使用视频作为指导,我所做的一切都完全相同,但我的不起作用 代码:

    <table id="myTable">
    <thead>
        <tr>
            <th>Title</th>
            <th>FirstName</th>
            <th>Surname</th>
            <th>Address1</th>
            <th>Address2</th>
            <th>Town</th>
            <th>Account1</th>
            <th>Account2</th>
        </tr>
    </thead>
</table>
@section mydataTable{
<script type="text/javascript" charset="utf8" 
src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.js"></script>
<script>
    $(document).ready(function () {
        $('#myTable').DataTable(
            {
                "processing": true,
                "filter": false,
                "serverSide": true,
                "paging": false,
                "responsive": true,
                "ajax":
                {
                    "url": "@Url.Action("LoadData")",
                    "datatype": "json",
                    "type": "POST",
                },
                "columnDefs": [
                    { "defaultContent": "-", "targets": "_all" },
                    { "width": "auto", "targets": 0, "orderable": false },
                    { "width": "auto", "targets": 1, "orderable": false },
                    { "width": "auto", "targets": 2, "orderable": false },
                    { "width": "auto", "targets": 3, "orderable": false },
                    { "width": "auto", "targets": 4, "orderable": false },
                    { "width": "auto", "targets": 5, "orderable": false },
                    { "width": "auto", "targets": 6, "orderable": false },
                    { "width": "auto", "targets": 7, "orderable": false },

                ],
                "columns": [
                    { "data": "Title", "name": "CTitle" },
                    { "data": "FirstName", "name": "CFirstName" },
                    { "data": "Surname", "name": "CSurname" },
                    { "data": "Address1", "name": "CAddress1" },
                    { "data": "Address2", "name": "CAddress2" },
                    { "data": "Town", "name": "CTown" },
                    { "data": "Account1", "name": "CAccount1" },
                    { "data": "Account2", "name": "CAccount2" },
                ],

            });
    });
</script>

}

C#:

    public JsonResult LoadData()
    {
        IEnumerable<DeathClaims> deathclaims = GetDc();
        return Json(new { data = deathclaims, recordsFiltered = deathclaims.Count(), recordsTotal = deathclaims.Count() });
    }

    private IEnumerable<DeathClaims> GetDc()
    {
        List<DeathClaims> deathlist = new List<DeathClaims>()
        {
            new DeathClaims {
                            Title = "Mr",
                            FirstName = "Michael",
                            Surname = "Smith",
                            Address1 = "132 Spalding Road",
                            Address2 = "TS252JP",
                            Town = "Hartlepool",
                            Account1 = "Current Account 1.0%",
                            Account2 = "Super Saver 3.0%"},
            new DeathClaims {
                            Title = "Mr",
                            FirstName = "Steve",
                            Surname = "Smith",
                            Address1 = "1 Something Close",
                            Address2 = "TS273QQ",
                            Town = "Hartlepool",
                            Account1 = "Current Account 1.0%",
                            Account2 = "Super Saver 2.0%"}
        };
        return deathlist;
    }

没有任何数据通过 - 我确定这是一个小问题,但我只是看不到它 提前感谢您的帮助

谢谢

【问题讨论】:

  • 如果你在 LoadData 里面放了一个断点,它会命中方法吗?尝试在脚本标签内准备好文档之外的数据表初始化。
  • 发送到您的 DataTable 的 JSON 的整体结构是什么?如果它类似于{ "deathlist": [ { ... }, { ... }, ... ] },那么您的DataTables ajax 部分需要使用dataSrc 选项:"dataSrc": "deathlist"。有关更多背景信息,请参阅here

标签: c# asp.net datatables


【解决方案1】:

让 DataTables 插件在服务器端工作。您需要在 LoadData() 方法中返回 JSON,如下所示,即

...

// Initialization.
string search = Request.Form.GetValues("search[value]")[0];  
string draw = Request.Form.GetValues("draw")[0];  
string order = Request.Form.GetValues("order[0][column]")[0];  
string orderDir = Request.Form.GetValues("order[0][dir]")[0];  
int startRec = Convert.ToInt32(Request.Form.GetValues("start")[0]);  
int pageSize = Convert.ToInt32(Request.Form.GetValues("length")[0]);  

...

// Process your code.

...

JsonResult result = this.Json(new { draw = Convert.ToInt32(draw), recordsTotal = totalRecords, recordsFiltered = recFilter, data = data }, JsonRequestBehavior.AllowGet);  

return result;
    
...

上述DataTables插件的属性需要自己在代码中捕获和维护。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-15
    • 2015-09-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 2018-08-06
    • 2016-02-07
    相关资源
    最近更新 更多