如何解决数据表-请求的未知参数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数据的制动点: 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%"
});
});
结果:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。