簡體   English   中英

C#MVC2 Jqgrid-做服務器端分頁的正確方法是什么?

[英]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);

而且我對問題所在有點迷茫:

  • 我是否正確設置了jqgrid分頁選項?
  • 我是否寫過不好的Linq不管拉什么都行? 例如,Sum()是否會導致所有行都被讀取?
  • 我做錯了.Skip()。Take()嗎?
  • 我是否完全想念其他東西?

編輯

將我的代碼與Oleg發布的示例進行比較時,我可以看到我按以下順序進行操作:

  1. 得到所有
  2. 選擇模型字段

Wheras Olegs示例似乎按以下順序進行:

  1. 得到所有
  2. 選擇模型字段

因此,我已更改為更簡單的實現:

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

您應該發布更多完整的代碼。 例如,當前代碼中未定義model.ToGridData 還不清楚如何計算歸因參數儀等的index 只有model.ToGridData()可以說程序產生的輸出是否與您定義的jsonReader相對應。

我建議您看一下這個既使用分頁又使用排序的舊答案。 另一個答案中,您將找到更多有關代碼示例的參考。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM