【发布时间】:2022-06-14 03:50:31
【问题描述】:
我正在使用数据表,我的 get 处理程序返回一个 json 结果,但数据表没有显示它。我错过了什么?这是我的代码。我正在使用带有剃须刀页面的 Visual Studio 2019 3.1 .net 核心。我试图在 OnGet 处理程序中调用我的数据表。我试过这个帖子,但也没有用。
我的cust 班级:
public class Cust
{
public int Id { get; set; }
public string Name { get; set; }
public string PhoneNumber { get; set; }
public string Address { get; set; }
public string PostalCode { get; set; }
}
_layout.cshtml:
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
<footer class="border-top footer text-muted">
<div class="container">
© 2022 - testApp - <a asp-area="" asp-page="/Privacy">Privacy</a>
</div>
</footer>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.12.1/datatables.min.css" />
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.12.1/datatables.min.js"></script>
@RenderSection("Scripts", required: false)
我的Index.cshtml.cs型号代码:
public class IndexModel : PageModel
{
[BindProperty]
public int Draw { get; set; }
// public IEnumerable<Column> Columns { get; set; }
// public IEnumerable<Order> Order { get; set; }
public int Start { get; set; }
public int Length { get; set; }
public List<Cust> Customers = new List<Cust>();
public JsonResult OnGet()
{
var recordsTotal = 3;
Cust cs1 = new Cust();
cs1.Id = 1;
cs1.Name = "test1";
cs1.Address = "address1";
cs1.PhoneNumber = "11111";
cs1.PostalCode = "1111";
Cust cs2 = new Cust();
cs2.Id = 1;
cs2.Name = "test2";
cs2.Address = "address1";
cs2.PhoneNumber = "11111";
cs2.PostalCode = "1111";
Cust cs3 = new Cust();
cs3.Id = 1;
cs3.Name = "test3";
cs3.Address = "address1";
cs3.PhoneNumber = "11111";
cs3.PostalCode = "1111";
Customers.Add(cs1);
Customers.Add(cs2);
Customers.Add(cs3);
var recordsFiltered = Customers.Count();
var data = Customers;
return new JsonResult(new
{
Draw = Draw,
RecordsTotal = recordsTotal,
RecordsFiltered = recordsFiltered,
Data = data
});
}
我的 Razor 页面 -- Pages/customers/Index.cshtml:
@page
@model testApp.Pages.Customer.IndexModel
@{
}
<h1>Index</h1>
<p>
</p>
<table id="myTable" class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Customers[0].Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Customers[0].Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Customers[0].PhoneNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.Customers[0].Address)
</th>
<th>
@Html.DisplayNameFor(model => model.Customers[0].PostalCode)
</th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
@section Scripts {
<script>
$(document).ready(function () {
$('#myTable').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
url: "/customers/Index?handler=OnGet",
"type": "GET",
"dataType": "json"
},
"columns": [
{ "data": "id", "autowidth": true },
{ "data": "name", "autowidth": true },
{ "data": "phoneNumber", "autowidth": true },
{ "data": "address", "autowidth": true },
{ "data": "postalCode", "autowidth": true }
],
"order": [[0, "desc"]]
});
});
</script>
}
这是我的 json 响应。
{
"draw": 0,
"recordsTotal": 3,
"recordsFiltered": 3,
"data": [
{
"id": 1,
"name": "test1",
"phoneNumber": "11111",
"address": "address1",
"postalCode": "1111"
},
{
"id": 1,
"name": "test2",
"phoneNumber": "11111",
"address": "address1",
"postalCode": "1111"
},
{
"id": 1,
"name": "test3",
"phoneNumber": "11111",
"address": "address1",
"postalCode": "1111"
}
]
}
我只是用这个项目测试它,看看我是否能让数据表工作。我最终会从数据库中获取数据。是的,体积会很大,有数千行。
【问题讨论】:
-
您能否edit 向我们展示 JSON 响应的文本?因为您使用的是
"serverSide": true,所以您需要确保该JSON 的结构是what DataTables expects it to be。 -
(另外,您是否有如此大量的数据需要来使用服务器端处理?)
-
非常感谢您的回复,我已将 json 结果添加到我的问题中。感谢您的帮助。
标签: asp.net-core razor datatables