asp.net - 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> <a onclick=delete('admin/deletebydataapijson?id='+${data}) class='btn btn-danger text-white' style='cursor: pointer'>Usuń</a></div>`
}
}
],
"width": "100%"
});
});
带有 Json 数据的刹车点: 这里
错误后的数据表: 这里
解决方案
列名应为小写:
$(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%"
});
});
结果:
推荐阅读
- node.js - npm 错误“Keccak 绑定编译失败。” 在安装松露时
- javascript - Appium测试android应用程序时如何修复错误“TypeError:client.click不是函数”?
- sql - 用于将 Redshift 数据移动到 Azure SQL 数据仓库的分发方法
- list - 带有 digitalWriteFast 的多个电机
- java - 如何修复“HTTP 状态 404”Spring MVC
- javascript - 在 React 中访问变量
- javascript - CKEDITOR:使用 GetData 方法时如何限制在 BR 标记前后添加默认空格
- java - 如何在 imageview 上创建随机的、无尽的动画
- java - spring websocket,如何实现@sendtouser
- ios - [MapPoint updateFramerate]:发送到实例的无法识别的选择器