【问题标题】:DataTables - Requested unknown parameter (ASP.NET Core MVC)DataTables - 请求的未知参数(ASP.NET Core MVC)
【发布时间】:2020-11-03 15:13:34
【问题描述】:

我遇到了 DataTables 的问题,因为它不读取我的 Json 数据。它说:

DataTables 警告:表 id=exerciseTable - 请求第 0 行第 0 列的未知参数“ExerciseId”。

在此错误之后,它会生成 2 行空数据(因此它知道该表中有 2 条记录)。我发现许多线程与我的相似,但没有任何帮助。你知道出了什么问题吗?提前致谢!

控制器:

[HttpGet]
public IActionResult GetAllDataApiJson()
{
    var data = DbContext.Exercises.ToList();
            
    return new JsonResult(data);
}

型号:

public class Exercise
{
    public int ExerciseId { get; set; }
    [Required]
    public string Name { get; set; }
    public string Desc { get; set; }
    [DataType("varbinary(max)")]
    public byte[] Photo { get; set; }
}

查看:

    <table id="exerciseTable" class="table table-striped border">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Description</th>
                <th>Photo</th>
                <th>Actions</th>
            </tr>
        </thead>
    </table>

脚本:

<script>
    $(document).ready(function () {
        $("#exerciseTable").DataTable({
            ajax: {
                "url": "/Admin/GetAllDataApiJson",
                "dataSrc": "",
                "type": "GET",
                "datatype": "json"
            },
            columns: [
                { "data": 'ExerciseId' },
                { "data": "Name" },
                { "data": "Desc" },
                { "data": "Photo" },
                {
                    "data": "id",
                    "width": "20%",
                    "render": function (data) {
                        return `<div class='text-center'><a class='btn btn-success' href='/admin/edit?id=${data}'>Edytuj</a> &nbsp; <a onclick=delete('admin/deletebydataapijson?id='+${data}) class='btn btn-danger text-white' style='cursor: pointer'>Usuń</a></div>`
                    }
                }
            ],
            "width": "100%"    
        });
    });

带有 Json 数据的刹车点: Here

出错后的数据表: Here

【问题讨论】:

  • 你能告诉我们实际返回的 JSON(不是被序列化为 JSON 的对象)
  • 看起来像这样:"[{\"ExerciseId\":4,\"Name\":\"rerererer\",\"Desc\":null,\"Photo\": null},{\"ExerciseId\":5,\"Name\":\"adaaadadadadada\",\"Desc\":\"opis opis\",\"Photo\":null},

标签: asp.net asp.net-core model-view-controller datatables


【解决方案1】:

列名应为小写:

$(document).ready(function () {
    $("#exerciseTable").DataTable({
        ajax: {
            "url": "/Admin/GetAllDataApiJson",
            "dataSrc": "",
            "type": "GET",
            "datatype": "json"
        },
        columns: [
            { "data": 'exerciseId' },
            { "data": "name" },
            { "data": "desc" },
            { "data": "photo" },
            {
                "data": "id",
                "width": "20%",
                "render": function (data) {
                    return `<div class='text-center'><a class='btn btn-success' href='/admin/edit?id=${data}'>Edytuj</a> &nbsp; <a onclick=delete('admin/deletebydataapijson?id='+${data}) class='btn btn-danger text-white' style='cursor: pointer'>Usuń</a></div>`
                }
            }
        ],
        "width": "100%"    
    });
});

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 2018-02-26
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多