DataTables - 请求的未知参数(ASP.NET Core MVC)
DataTables - Requested unknown parameter (ASP.NET Core MVC)
我遇到了 DataTables 的问题,因为它不读取我的 Json 数据。它说:
DataTables warning: table id=exerciseTable - Requested unknown parameter 'ExerciseId' for row 0, column 0.
在这个错误之后,它生成了 2 行空数据(所以它知道在这个 table 中有 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> <a onclick=delete('admin/deletebydataapijson?id='+${data}) class='btn btn-danger text-white' style='cursor: pointer'>Usuń</a></div>`
}
}
],
"width": "100%"
});
});
断点 Json 数据:
Here
错误后的数据表:
Here
列名应小写:
$(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> <a onclick=delete('admin/deletebydataapijson?id='+${data}) class='btn btn-danger text-white' style='cursor: pointer'>Usuń</a></div>`
}
}
],
"width": "100%"
});
});
结果:
我遇到了 DataTables 的问题,因为它不读取我的 Json 数据。它说:
DataTables warning: table id=exerciseTable - Requested unknown parameter 'ExerciseId' for row 0, column 0.
在这个错误之后,它生成了 2 行空数据(所以它知道在这个 table 中有 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> <a onclick=delete('admin/deletebydataapijson?id='+${data}) class='btn btn-danger text-white' style='cursor: pointer'>Usuń</a></div>`
}
}
],
"width": "100%"
});
});
断点 Json 数据: Here
错误后的数据表: Here
列名应小写:
$(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> <a onclick=delete('admin/deletebydataapijson?id='+${data}) class='btn btn-danger text-white' style='cursor: pointer'>Usuń</a></div>`
}
}
],
"width": "100%"
});
});
结果: