【问题标题】:My datatable is not picking the json result我的数据表没有选择 json 结果
【发布时间】: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">
            &copy; 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


【解决方案1】:

这是一个完整的工作演示,您可以关注:

页面(Pages/customers/Index.cshtml)

@page 
@model 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>

页面中的JS:

@section Scripts {
    <script>
        $(document).ready(function () {
            $.ajax({
                type: "GET",
                url: "/customers/Index?handler=Json",      //change here...
                dataType: "json",
                success: OnSuccess,
            });
        });
        function OnSuccess(response) {    
            console.log(response.data)
            $('#myTable').DataTable(
                {
                    data: response.data,
                    columns: [
                        { "data": "id" },
                        { "data": "name" },
                        { "data": "phoneNumber" },
                        { "data": "address" },
                        { "data": "postalCode" }
                    ],
                });
        };
    </script>
}

PageModel(Pages/customers/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 void OnGet()             //add this.....
    {

    }

    public JsonResult OnGetJson()   //change handler name..
    {
        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
        });

    }
}

_Layout.cshtml:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - RazorPagesProj3_1</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
    <header>
        //....
    </header>
    <div class="container">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; 2022 - RazorPagesProj3_1 - <a asp-area="" asp-page="/Privacy">Privacy</a>
        </div>
    </footer>

    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>
     <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)
</body>
</html>

【讨论】:

  • 谢谢瑞纳。它仍然不起作用。看来我的 jquery 可能有问题?当我在 OnGetJson 上设置断点并运行它时,它甚至不会去那里,页面只是返回表头。
  • 嗨,我也分享了整个_Layout.cshtml。如果这里有任何错误,请您在浏览器中按 F12 并检查Console 面板吗?
  • 非常感谢瑞纳。我意识到 jquery 标记在我的代码中不正确。它现在正在工作。
猜你喜欢
  • 1970-01-01
  • 2021-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-01
  • 2012-05-16
  • 2015-10-05
  • 1970-01-01
相关资源
最近更新 更多