簡體   English   中英

jqGrid不顯示數據,但分頁和列名顯示/工作正常

[英]jqGrid not displaying data but paging and column names are displayed/working fine

我一直在關注https://tpeczek.codeplex.com/上的教程,以使jqGrid工作,並在更新GetData()動作結果以啟用分頁和排序后,現在我的網格不再顯示數據,但是我不確定為什么不會發出任何錯誤。 曾經工作的代碼:

    public ActionResult GetData()
    {
        try
        {
            var model = (from s in db.Sections
                         select new
                         {
                             s.ID,
                             s.RouteName,
                             s.Title
                         }).ToList();
            return Json(model, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
            return Json(null, JsonRequestBehavior.AllowGet);
        }
    }

我的新代碼嘗試添加分頁和排序。

公共ActionResult GetData(字符串sidx,字符串sord,int頁面,int行){試試{int RowCount = db.Sections.Count(); int SkipCount =(頁*行);

            string OrderBy = (sidx + " " + sord);

            var SectionData = new
            {
                total = (int)Math.Ceiling((float)RowCount / (float)rows),
                page = page,
                records = RowCount,
                rows = (from s in db.Sections
                        select new
                        {
                            id = s.ID,
                            cell = new string[] {
                                SqlFunctions.StringConvert((double)s.ID).Trim(),
                                s.RouteName,
                                s.Title
                            }
                            .OrderBy(x => sidx)
                            .Skip(SkipCount)
                            .Take(rows)
                        }).ToArray()
            };
            return Json(SectionData, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
            return Json(null, JsonRequestBehavior.AllowGet);
        }

    }

編輯:jqGrid代碼:

<script type="text/javascript">
$( document ).ready( function ()
{
    $( '#Sections' ).jqGrid( {
        url: '/Admin/Section/GetData',
        datatype: 'json',
        mtype: 'GET',
        colNames: ['ID', 'RouteName', 'Title'],
        colModel: [
                    { name: 'ID', index: 'ID', width: '10' },
                    { name: 'RouteName', index: 'RouteName', width: '50' },
                    { name: 'Title', index: 'Title' }
        ],
        autowidth: true,
        height: '100%',
        pager: $( '#SectionsPager' ),
        rowNum: 10,
        sortname: 'ID',
        sortorder: 'asc',
        viewrecords: true
    } ).navGrid(
       '#SectionsPager',
       //enabling buttons
       { add: true, del: false, edit: false, search: false },
       //edit options
       { width: 'auto' },
       //add options
       { width: 'auto', url: '/Admin/Section/Add' },
       //delete options
       {} );
} );

因此,我最終在jqGrid配置中添加了命令loadonce: true來啟用客戶端排序,並刪除了服務器端處理排序的所有代碼。 我的網格現在正在顯示數據,並且可以進行排序和分頁。

您需要設置: datatype: "json" ,如果您使用的是loadOnce:trueloadOnce:true刪除。

暫無
暫無

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

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