2017-02-23 51 views
0

我在使用mvc的jqgrid中遇到服務器端分頁問題。這是我的控制器代碼。如何解決JQGrid中使用mvc4的服務器端分頁問題

[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

*「與服務器端分頁我現在面臨的問題......」 * - 你應該說明是什麼問題。 – jww

回答

0

以下是我所做的一些更改以及問題解決。

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

2-紀錄=

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