【发布时间】:2021-07-29 12:44:04
【问题描述】:
我收到以下错误。我知道有类似的已回答问题,但我已尝试使用这些答案,但仍然看不到我的错误。
错误信息:
我在表格和请求中有正确数量的列。我检查了错字。我正在使用视频作为指导,我所做的一切都完全相同,但我的不起作用 代码:
<table id="myTable">
<thead>
<tr>
<th>Title</th>
<th>FirstName</th>
<th>Surname</th>
<th>Address1</th>
<th>Address2</th>
<th>Town</th>
<th>Account1</th>
<th>Account2</th>
</tr>
</thead>
</table>
@section mydataTable{
<script type="text/javascript" charset="utf8"
src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.js"></script>
<script>
$(document).ready(function () {
$('#myTable').DataTable(
{
"processing": true,
"filter": false,
"serverSide": true,
"paging": false,
"responsive": true,
"ajax":
{
"url": "@Url.Action("LoadData")",
"datatype": "json",
"type": "POST",
},
"columnDefs": [
{ "defaultContent": "-", "targets": "_all" },
{ "width": "auto", "targets": 0, "orderable": false },
{ "width": "auto", "targets": 1, "orderable": false },
{ "width": "auto", "targets": 2, "orderable": false },
{ "width": "auto", "targets": 3, "orderable": false },
{ "width": "auto", "targets": 4, "orderable": false },
{ "width": "auto", "targets": 5, "orderable": false },
{ "width": "auto", "targets": 6, "orderable": false },
{ "width": "auto", "targets": 7, "orderable": false },
],
"columns": [
{ "data": "Title", "name": "CTitle" },
{ "data": "FirstName", "name": "CFirstName" },
{ "data": "Surname", "name": "CSurname" },
{ "data": "Address1", "name": "CAddress1" },
{ "data": "Address2", "name": "CAddress2" },
{ "data": "Town", "name": "CTown" },
{ "data": "Account1", "name": "CAccount1" },
{ "data": "Account2", "name": "CAccount2" },
],
});
});
</script>
}
C#:
public JsonResult LoadData()
{
IEnumerable<DeathClaims> deathclaims = GetDc();
return Json(new { data = deathclaims, recordsFiltered = deathclaims.Count(), recordsTotal = deathclaims.Count() });
}
private IEnumerable<DeathClaims> GetDc()
{
List<DeathClaims> deathlist = new List<DeathClaims>()
{
new DeathClaims {
Title = "Mr",
FirstName = "Michael",
Surname = "Smith",
Address1 = "132 Spalding Road",
Address2 = "TS252JP",
Town = "Hartlepool",
Account1 = "Current Account 1.0%",
Account2 = "Super Saver 3.0%"},
new DeathClaims {
Title = "Mr",
FirstName = "Steve",
Surname = "Smith",
Address1 = "1 Something Close",
Address2 = "TS273QQ",
Town = "Hartlepool",
Account1 = "Current Account 1.0%",
Account2 = "Super Saver 2.0%"}
};
return deathlist;
}
没有任何数据通过 - 我确定这是一个小问题,但我只是看不到它 提前感谢您的帮助
谢谢
【问题讨论】:
-
如果你在 LoadData 里面放了一个断点,它会命中方法吗?尝试在脚本标签内准备好文档之外的数据表初始化。
-
发送到您的 DataTable 的 JSON 的整体结构是什么?如果它类似于
{ "deathlist": [ { ... }, { ... }, ... ] },那么您的DataTablesajax部分需要使用dataSrc选项:"dataSrc": "deathlist"。有关更多背景信息,请参阅here。
标签: c# asp.net datatables