首页 > 解决方案 > DataTables - 请求的未知参数(ASP.NET Core MVC)

问题描述

我遇到了 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 数据的刹车点: 这里

错误后的数据表: 这里

标签: asp.netasp.net-coremodel-view-controllerdatatables

解决方案


列名应为小写:

$(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%"    
    });
});

结果:

在此处输入图像描述


推荐阅读