【问题标题】:jqGrid not displaying data but paging and column names are displayed/working finejqGrid 不显示数据,但显示分页和列名/工作正常
【发布时间】:2013-12-27 23:40:58
【问题描述】:

我一直在关注https://tpeczek.codeplex.com/ 的教程以使 jqGrid 正常工作,并在更新我的 GetData() actionresult 以启用分页和排序之后,现在我的网格不再显示数据,但我不确定为什么没有出现错误抛出。曾经工作的代码:

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

我的新代码尝试添加分页和排序。

public ActionResult GetData(string sidx, string sord, int page, int rows) { 尝试 { int RowCount = 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
       {} );
} );

【问题讨论】:

  • @Eagle..请显示 jqgrid 的代码..可能有错误
  • @Avinash 很抱歉,因为匆忙出去工作而忘记了。我现在已经添加了。

标签: c# jqgrid asp.net-mvc-5


【解决方案1】:

所以我最终将命令 loadonce: true 添加到 jqGrid 配置中以启用客户端排序,并删除了服务器端处理排序的所有代码。我的网格现在可以正常显示数据和排序和分页。

【讨论】:

  • @Matthew..首先,jqgrid有两种排序方式。服务器端和客户端。您没有提到您的任何要求,即您想要客户端还是服务器端。其次,由于您的代码类似于服务器端分页,因此我添加了答案。因此,请在投反对票之前三思,在问题中正确提及您的要求。您提出的问题,以及您为此回答的答案,肯定会误导许多 SO 用户。这是因为,您的问题涉及服务器端分页,而答案是客户端分页。
  • @Avinash 我对您投了反对票,因为您添加 datatype: "json" 的建议已经在我的代码中,并且您建议在我的代码不存在时从我的代码中删除 loadOnce: true。您的回答没有提供对问题或解决方案的深入了解。我的问题中确实有服务器端代码,但不需要在服务器端或客户端上都有它,所以我的回答确实解决了让分页工作的问题。关于 SO 有很多问题,人们试图以一种方式做事,而最好的答案是另一种方式,这将是其中之一。
【解决方案2】:

您需要设置:datatype: "json",如果您使用的是loadOnce:true,请将其删除..

【讨论】:

  • 根据 jqGrid 文档,loadOnce 的默认设置是 false。
猜你喜欢
  • 2015-01-25
  • 2012-06-09
  • 2014-08-06
  • 2014-06-27
  • 1970-01-01
  • 2012-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多