等待大量数据时,DataTables错误500

时间:2015-10-21 10:16:27

标签: c# asp.net-mvc knockout.js datatables

我有一个使用Knockout.JS和Knockout-mapper.js的DataTable实现。我用ajax调用获取我的数据,它与5k记录很好地配合。但是,当我试图说出100k记录时,我得到了

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来自控制器(100k结果)

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;
    }

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可能达到MaxJsongLength限制。尝试将操作方法​​更改为

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;

}

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

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