当等待大量数据时,数据表错误500

本文关键字:数据表 错误 数据 等待 | 更新日期: 2023-09-27 18:18:35

我有一个数据表实现Knockout.JS和Knockout-mapper.js。我取我的数据与ajax调用,它工作得很好,说5k记录。但是当我尝试获取100,000条记录时,我得到

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

有没有人能给我指出正确的方向,我怎样才能在我的网格中获得大量的数据?

页面:

    <script type="text/javascript">
    $(function () {
            function viewModel(data) {
                var self = this;
                ko.mapping.fromJS(data, {}, self);
            }
            $.ajax({
                url: "@Url.Action("GetRecordsJsonResultAll")", success: function (data) {
                    ko.applyBindings(new viewModel(data));
                    $("#items").DataTable({
                        responsive: true
                    });
                }
            });
        });
</script>
<div class="row">
    <table id="items" class="table table-striped table-hover table-bordered">
        <thead>
            <tr>
                <th>ID</th>
                <th>First name</th>
                <th>Last name</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody data-bind="foreach: items">
            <tr>
                <td><span data-bind="text: $data.Id"></span></td>
                <td><span data-bind="text: $data.FirstName"></span></td>
                <td><span data-bind="text: $data.LastName"></span></td>
                <td><span data-bind="text: $data.Email"></span></td>
            </tr>
        </tbody>
    </table>
</div>

Json result from controller (100k results)

public virtual JsonResult GetRecordsJsonResultAll()
    {
        var userBusinessLogic = InterfaceResolver.ResolveWithTransaction<IUserBusinessLogic>();
        var records = userBusinessLogic.GetAll().Select(x => new
        {
            x.Id,
            x.FirstName,
            x.LastName,
            x.Email
        }).OrderBy(i => i.Id);
        var data = Json(new
        {
            max = records.Count(),
            items = records
        }, JsonRequestBehavior.AllowGet);
        return data;
    }

谢谢你的帮助。

当等待大量数据时,数据表错误500

您可能已经达到了MaxJsongLength的极限。尝试将action方法更改为

public virtual ActionResult GetRecordsJsonResultAll()
{
    var userBusinessLogic = InterfaceResolver.ResolveWithTransaction<IUserBusinessLogic>();
    var records = userBusinessLogic.GetAll().Select(x => new
    {
        x.Id,
        x.FirstName,
        x.LastName,
        x.Email
    }).OrderBy(i => i.Id);
    var serializer = new JavaScriptSerializer();
    serializer.MaxJsonLength = Int32.MaxValue;
    var data = new
    {
        max = records.Count(),
        items = records
    };
    var result = new ContentResult
    {
        Content = serializer.Serialize(data),
        ContentType = "application/json"
    };
    return result;
}

此外,使用敲除映射映射100000记录可能会导致网页整体缓慢/无响应的用户体验。根据需要使用分页或加载数据。您可以使用

进行测试
function viewModel(data) {
    var self = this;
    console.log(data, "from server")
    self.items = data.items.slice(0,1000);
}