Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# JQuery数据表服务器端分页_C#_Jquery_Asp.net_Pagination_Jquery Datatables - Fatal编程技术网

C# JQuery数据表服务器端分页

C# JQuery数据表服务器端分页,c#,jquery,asp.net,pagination,jquery-datatables,C#,Jquery,Asp.net,Pagination,Jquery Datatables,在我的web应用程序中,我使用JQuery插件显示从数据库检索到的数据 我目前正在使用客户端分页,但我的表中的数据正在大量增长,在ASP.NET页面中加载的速度现在有点慢。所以我计划切换到服务器端分页 我知道DataTables插件支持它,但四处搜索,我还没有发现关于实现它的任何细节 我的主要疑问是:如果我在服务器端实现分页,我还必须实现排序,或者我可以将其委托给客户端 你有过这样的经历吗 注意我使用Linq to SQL连接到我的数据库因为您使用的是Linq to SQL,所以分页非常简单:

在我的web应用程序中,我使用JQuery插件显示从数据库检索到的数据

我目前正在使用客户端分页,但我的表中的数据正在大量增长,在ASP.NET页面中加载的速度现在有点慢。所以我计划切换到服务器端分页

我知道DataTables插件支持它,但四处搜索,我还没有发现关于实现它的任何细节

我的主要疑问是:如果我在服务器端实现分页,我还必须实现排序,或者我可以将其委托给客户端

你有过这样的经历吗


注意我使用Linq to SQL连接到我的数据库

因为您使用的是Linq to SQL,所以分页非常简单:

var c = new MyDataContext("your string");

c.Employees.Skip(pageIndex * pageSize).Take(pageSize);
此代码将在服务器上有效地分页

我没有使用DataTables jQuery插件,但我假设您使用AJAX来获取数据(因为您没有使用MVC),所以只需将当前页面索引和每页的行数(页面大小)作为参数发送即可

为了满足要求,还需要在服务器上对查询进行排序,因此需要将排序条件发送到服务器并应用排序

要根据
字符串在服务器上订购,请检查以下问题:


派对有点晚了,但这值得分享:)

现有的答案可能适用于旧版本的dataTable,但当前版本(我使用的是1.10+)通过了开始记录和长度,因此任何建议
pageNo*pageSize
的内容都会给出错误的结果

第一种简单的“手动”方法 对于我想做的事情,接受的答案也非常复杂,经过一些调试后,我发现页面大小和开始记录只是作为Http
Request
名为
start
length
的值传递。文本搜索作为
search[value]
传递,排序顺序在名为
order[0][column]
的成员中传递,排序方向在
order[0][dir]
等中传递

我用于排序和筛选的基本代码如下所示:

从HTTP请求对象获取分页、排序和筛选值:

int startRec = 0;
int.TryParse(Request["start"], out startRec);
int pageSize = 10;
int.TryParse(Request["length"], out pageSize);
var search = Request["search[value]"];
var order = Request["order[0][column]"];
var direction = Request["order[0][dir]"];

var query = this._dataStore.Records.AsQueryable();
首先应用(不区分大小写)搜索:

if (!string.IsNullOrWhiteSpace(search))
{
    query = query.Where(x => x.Label.ToLower().Contains(search.ToLower()));
}
然后应用任何排序:

switch (order)
{
    // My id column
    case "0":
        query = (direction == "desc") ? query.OrderByDescending(x => x.Id) : query.OrderBy(x => x.Id);
        break;
    // My label column
    case "1":
        query = (direction == "desc") ? query.OrderByDescending(x => x.Label) : query.OrderBy(x => x.Label);
        break;
}
最后应用分页:

query = query.Skip(startRec).Take(pageSize);
现在可以返回正确的记录了

更新(使用“Datatables.net for MVC5”)

一旦我了解了服务器端数据表的基本知识,就应该开始寻找现有的插件/util来简化代码。到目前为止,我发现最适合MVC5的是nuget包

  • 安装NuGet软件包

  • 更改控制器操作以使用
    DataTablesBinder
    提供IDataTablesRequest接口

  • e、 g

  • 首先应用任何搜索筛选器:
  • e、 g

  • 可应用任何排序:
  • e、 g

  • 然后像前面一样使用
    Skip
    Take
    应用分页:
  • e、 g

  • 最后使用
    datatableresponse
    对象返回JSON结果:
  • e、 g

    这将所有的搜索、排序和分页简化为一个易于重复的模式


    .

    如何连接到数据库?Linq to SQL,EF,NH,DataSet?@Jupaol我正在使用Linq to SQL谢谢你的回答,这非常有用+1jQuery
    dataTable
    自动发送开始记录和页面大小,而不是页码。排序作为索引列名/值的集合发送。这个答案对于
    dataTable
    的使用具有误导性,并且仅适用于实现简单手动分页/排序的情况。jQuery
    dataTable
    发送开始记录,而不是页码。此外,排序顺序的发送方式要复杂得多。这个答案对于所问的问题是不正确的,对于那些发现它的人来说是误导性的。
    query = query.Skip(startRec).Take(pageSize);
    
     public JsonResult Table([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestmodel)
    
    if (!string.IsNullOrEmpty(requestmodel.Search.Value))
    {
        query = query.Where(x => x.CompanyTypeName.Contains(requestmodel.Search.Value) || x.CompanyTypeDescription.Contains(requestmodel.Search.Value));
    }
    
    foreach (var sort in requestmodel.Columns.GetSortedColumns())
    {
        switch (sort.Name)
        {
            case "CompanyTypeDescription":
                query = sort.SortDirection == Column.OrderDirection.Ascendant ? query.OrderBy(x => x.CompanyTypeDescription) : query.OrderByDescending(x => x.CompanyTypeDescription);
                break;
            case "CompanyTypeName":
            default:
                query = sort.SortDirection == Column.OrderDirection.Ascendant ? query.OrderBy(x => x.CompanyTypeName) : query.OrderByDescending(x => x.CompanyTypeName);
                break;
        }
    }
    
    var result = query.Skip(requestmodel.Start).Take(requestmodel.Length).Select(x => new { x.CompanyTypeName, x.CompanyTypeDescription });
    
    return Json(new DataTablesResponse(requestmodel.Draw, result, query.Count(), base.RefSureContext.CompanyType.Count()), JsonRequestBehavior.AllowGet);