2013-12-10 47 views
0

我一直在遵循教程在https://tpeczek.codeplex.com/获取jqGrid工作和更新我的GetData()actionresult以启用分页和排序,现在我的网格不再显示数据,但我不知道为什么没有错误抛出抛出。代码曾经工作:jqGrid不显示数据,但分页和列名显示/工作正常

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行数= db.Sections.Count(); int SkipCount =(page * rows);

  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 
     {}); 
}); 

+0

@ Eagle..please为jqgrid..there显示的代码可能是错误 –

+0

@Avinash对此感到遗憾被冲出工作,忘了。我现在添加了它。 – Matthew

回答

-1

所以我结束了添加命令loadonce: true到的jqGrid配置,以使客户机侧排序和已删除在服务器端处理所有代码排序。我的网格现在显示的数据和排序和分页罚款。

+0

@ Matthew..Firstly,也有jqGrid的分类有两种方式。服务器端和客户端。你没有提到你的需求,即你想要客户端还是服务器端。其次,由于你的代码类似于服务器端分页,我添加了答案。所以,在你投降之前请考虑一下,在问题中提到你的要求。您提出的问题以及您回答的答案会明确地误导SO的许多用户。有趣的是,你的问题涉及服务器端分页,而答案是客户端分页。 –

+0

@Avinash我下来投你,因为你的建议添加的'数据类型:“JSON”'已经在我的代码,你的建议删除'loadOnce:从我的代码TRUE'当它不存在。您的回答没有提供有关问题或解决方案的信息。我的问题确实有它的服务器端的代码,但没有要求有它在服务器或客户端上的,所以我的回答也得到解决分页工作的问题。对于人们试图以某种方式做事情的问题,有很多问题,最好的答案是再做一次,这将是其中的一次。 – Matthew

-1

您需要设置:datatype: "json",如果在你使用loadOnce:true删除..

+0

根据jqGrid文档,loadOnce的默认设置为false。 – Matthew