【问题标题】:How to use Skip() and Take() with JQuery DataTables and generic method如何将 Skip() 和 Take() 与 JQuery DataTables 和通用方法一起使用
【发布时间】:2019-01-23 19:01:08
【问题描述】:

我在 ASP.NET MVC 5 中有一个视图,它一次返回一个实体的所有记录,超过 40,000 条记录。 (延迟加载已启用,因此它返回的数据不止于此。)正如预期的那样,我总是收到内存不足异常。所以我需要限制显示的数据,但在页面更改或执行搜索时保​​留所有可显示的数据。

这是查询数据源以检索结果的通用方法。 Manager.AllAsync 是一种通用实体框架方法,用于查询通过 TEntity 提供的实体:

public async Task<ActionResult> DataTableAsync<TEntity>(AlertModel alert, string viewName = null) where TEntity : class
    {
        // get data
        var data = (from a in await Manager.AllAsync<TEntity>()
                    select a).ToList();

       // create model with alert
        var response = new TableModel<TEntity> 
        {
            Alert = alert,
            Data = data
        };

        // return partial data view
       return  PartialView(viewName ?? $"_{typeof(TEntity).Name}Table", response);

    }

这里是TableModel的定义

public class TableModel<TEntity>
{
    /// <summary>
    /// Alert for the model.
    /// </summary>
    public AlertModel Alert { get; set; }

    /// <summary>
    /// Data from the database.
    /// </summary>
    public IList<TEntity> Data { get; set; }
}

这里是AllAsync的定义

 public virtual async Task<IList<T>> AllAsync<T>() where T : class
    {
        try
        {
            return await Context.Set<T>().ToListAsync();
        }
        catch (ArgumentNullException ex)
        {
            throw new InvalidOperationException($"Invalid state: {typeof(T).Name} DbSet is null.", ex);
        }
    }

然后视图只是使用foreach 遍历记录。

这是通用 DataTables JavaScript 的配置方式:

function makeDataTable(id) {
var selector = '#' + id;
$(selector).DataTable({
    order: [],
    search: {
        smart: false
    },
    stateSave: true,
    deferRender: true,
    columnDefs: [{
        targets: 'no-sort',
        orderable: false
    },
    {
        targets: 'date-col',
        type: 'date',
        orderSequence: [
            'desc',
            'asc'
        ]
    }],
    lengthMenu: [
        [10, 15, 20, -1],
        [10, 15, 20, 'All']
    ]
});
$(selector).width('100%');
}

我不确定 DataTables Show 选项如何与方法通信以一次仅显示这么多,或者如何在不遇到 out of memory 异常的情况下 Show All。我知道 DataTables 有 Server Side Processing 但现在检索数据的方式是 Linq to Entities(来自数据源方法)。我发现一些文章解释了我应该做什么Like this one for example,但这篇文章返回了JsonResult。让这个方法不那么通用会不会更容易?

【问题讨论】:

  • 这个问题很宽泛,做个get page方法,传入page和page size...使用Server Side Processing,传入查询参数
  • 这个问题有多宽泛?它实际上是非常具体的......如何将skip、take、页码和显示每页记录数参数传递给JQuery DataTables。
  • 它太宽泛了,因为您向我们提出了一个要完全回答的问题,用户需要 1. 编写分页架构(因为您只支持 get all)。 2. 研究或具备JQuery DataTables的知识。 3. 编写客户端请求并创建存储分页变量的架构。
  • DataTables 已经支持分页架构。服务器分页 - 关于 DataTables 库 - 不会最大化内存是我面临的问题。
  • 这个问题是您从存储库中的数据库中提取所有内容(反)模式,您需要更改以公开一个接受跳过并在 iqueryable 而不是 ienumerable 上使用它的函数

标签: c# asp.net-mvc-5 datatables


【解决方案1】:

您可以通过多种方式解决此问题,但我会给您一个链接 给你一些灵感来解决你的问题,我是我的 最后一个解决方案我只创建这样的模型

Interface IDatatables where T: class { string request URL{get; 放; }

ICollection 列按钮编辑 {get;设置;}
按钮编辑 {get;设置;}等..

}

在您的 ActionResult 中,您需要构建一个与 数据表的请求参数这里有一些你可以找到的链接 与数据表集成的一些灵感 https://www.c-sharpcorner.com/article/using-datatables-grid-with-asp-net-mvc/ https://gist.github.com/OllieJones/7448933cc85ee740e990383e4fded412

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-28
    • 2018-02-03
    • 2010-10-20
    • 2018-03-05
    • 1970-01-01
    • 2016-10-03
    • 2015-04-28
    • 1970-01-01
    相关资源
    最近更新 更多