如何使用mvc4解决JQGrid中的服务器端分页问题

时间:2017-02-23 05:25:39

标签: asp.net-mvc linq pagination jqgrid

我在使用mvc的jqgrid中遇到服务器端分页问题。这是我的控制器代码。

[HttpGet]
public JsonResult GetAllStudents(JqGridRequest jRequest) {
    var Records =  from a in entities.studentdetails.ToList() select new {
        a.ID,
        a.Name,
        a.DOB
    };
    int pageIndex = Convert.ToInt32(jRequest.PageIndex);
    int pageSize = jRequest.RecordsCount;
    int startRow = (pageIndex * pageSize) + 1;
    int totalRecords = Records.Count();
    int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
    Records = Records.Skip(pageIndex * pageSize).Take(pageSize).ToArray();    
    if (jRequest.Searching) {
        int sId = Convert.ToInt32(jRequest.SearchingFilters.Filters[0].SearchingValue);
        //string sName = jRequest.SearchingFilters.Filters[0].SearchingValue;
        Records = entities.studentdetails.Select(a => new {
            a.ID,
            a.Name,
            a.DOB
        }).Where(p => p.ID == sId);
    }
    else {
       Records = entities.studentdetails.Select(a => new {
           a.ID,
           a.Name,
           a.DOB
       });
    }
    var jsonData = new {
        total = totalPages,
        page = pageIndex,
        records = totalRecords,
        rows = Records
    };
    return Json(jsonData, JsonRequestBehavior.AllowGet); 
}

1 个答案:

答案 0 :(得分:0)

以下是我所做的一些更改,问题也解决了。

1- int pageIndex = Convert.ToInt32(jRequest.PageIndex) + 1 ;

2- Records =

entities.studentdetails.Select(
                a => new
                {
                    a.ID,
                    a.Name,
                    a.DOB
                })**.OrderBy(s => s.ID).Skip((pageIndex-1) *   pageSize).Take(pageSize).ToArray();**