[英]C# MVC2 Jqgrid - what is the correct way to do server side paging?
我有一個jqgrid,其中數據庫表有幾千行,但是jqrid一次只顯示15行。
它應該顯示非常快(查詢15行不需要很長時間)。 但是相反,它需要10到20秒,這表明它每次都在檢索整個表。
網格的定義如下:
$("#Products").jqGrid({
url: url, mtype: "get", datatype: "json", jsonReader: {
root: "Rows", page: "Page", total: "Total", records: "Records", repeatitems: false,
userdata: "UserData",id: "Id"},
colNames: ["Product Id","Product Code", ... etc ],
colModel: [{ name: "Id", ... etc}],
viewrecords: true, height: 400, width: 800, pager: $("#jqgPager"),
rowNum: 15, rowList: [50, 100, 200],
autowidth: true, multiselect: false
服務器端(MVC2操作)執行以下操作:
var model = (from p in products
select new
{
p.Id, p.ProductCode, p.ProductDescription,
AllocatedQty = p.WarehouseProducts.Sum(wp => wp.AllocatedQuantity),
QtyOnHand = p.WarehouseProducts.Sum(wp => wp.OnHandQuantity)
}).AsQueryable();
JsonResult json = Json(model.ToGridData(
page, rows, orderBy, "",
new[] { "Id", "ProductCode", "ProductDescription", "AllocatedQty", "QtyOnHand" }),
JsonRequestBehavior.AllowGet);
最后,model.ToGridData擴展方法執行此操作:
var data =_service.GetAll();
var page = data.Skip((index) * pageSize).Take(pageSize);
list.Add(page.AsEnumerable);
而且我對問題所在有點迷茫:
編輯
將我的代碼與Oleg發布的示例進行比較時,我可以看到我按以下順序進行操作:
Wheras Olegs示例似乎按以下順序進行:
因此,我已更改為更簡單的實現:
public ActionResult GetProductList(int page, int rows, string sidx, string sord,
string searchOper, string searchField, string searchString)
{
List<Product> products = _productService.GetAllProducts();
int totalRecords = products.Count();
var pagedData = products.Skip((page > 0 ? page - 1 : 0) * rows).Take(rows);
var model = (from p in pagedData
select new
{
p.Id, p.ProductCode, p.ProductDescription,
Barcode = string.Empty, UnitOfMeasure = string.Empty,
p.PackSize, AllocatedQty = string.Empty,
QtyOnHand = string.Empty }).ToList();
var jsonData = new
{
total = page, records = totalRecords,
page = (totalRecords + rows - 1) / rows, rows = model
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
但是,這有一個新問題:
A circular reference was detected while serializing an object of type
'System.Data.Entity.DynamicProxies.Product_FA935D3899E2...
我現在可以通過Oleg的示例看到的唯一區別是,他的getAll返回IQueryable
,而我的只是List
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.