jQuery DataTable Scroller Ajax对MVC控制器的请求失败

时间:2018-08-12 10:06:50

标签: c# jquery ajax asp.net-mvc datatables

我正在尝试使用JQuery DataTable Scroller加载大数据,如下所示: https://datatables.net/extensions/scroller/examples/initialisation/server-side_processing.html 以下是我的DataTable初始化代码: HTML代码:

<table id="example" class="display nowrap" style="width:100%">
    <thead>
        <tr>
            <th>ID</th>
            <th>First name</th>
            <th>Last name</th>
            <th>ZIP / Post code</th>
            <th>Country</th>
        </tr>
    </thead>
</table>

jQuery代码:

$(document).ready(function () {
                $('#example').DataTable({
                    serverSide: true,
                    ordering: false,
                    searching: false,
                    ajax: {
                        "url": "@Url.Action("GetData", "ClaimPOC")",
                        "type": "POST",
                        "datatype": "json"
                    },
                    "columns": [
                        { "data": "ID", "name": "ID", "autoWidth": true },
                        { "data": "FirstName", "name": "FirstName", "autoWidth": true },
                        { "data": "LastName", "name": "LastName", "autoWidth": true },
                        { "data": "PostCode", "name": "PostCode", "autoWidth": true },
                        { "data": "Country", "name": "Country", "autoWidth": true }
                    ],
                    scrollY: 200,
                    scroller: {
                        loadingIndicator: true
                    },
                    stateSave: true
                });
            });

我正在使用以下控制器方法从20,000个项目的大型列表中获取数据:

public ActionResult GetData()
        {
            var draw = Request.Form.GetValues("draw").FirstOrDefault();
            var start = Request.Form.GetValues("start").FirstOrDefault();
            var length = Request.Form.GetValues("length").FirstOrDefault();
            int recordsTotal = 1;
            var data = new List<ResData>();
            for (int i = 0; i < 20000; i++)
            {
                data.Add(new ResData() { ID = i, Country = "US" + i, FirstName = "John" + i, LastName = "Cena" + i, PostCode = "61546" + i });
            }
            recordsTotal = data.Count;
            return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data }, JsonRequestBehavior.AllowGet);
        }

        class ResData
        {
            public int ID { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string PostCode { get; set; }
            public string Country { get; set; }
        }

我不熟悉DataTable中的Ajax加载。对于这些问题,我在DataTable文档中找不到很好的解释:

  1. 是否要从数据集中开始索引项目并确定项目总数?
  2. 添加20,000个项目时,代码返回Ajax错误,但添加1个项目时未返回错误。为什么会这样?

0 个答案:

没有答案