简体   繁体   中英

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

I have been following the tutorials at https://tpeczek.codeplex.com/ to get jqGrid working and after updating my GetData() actionresult to enable paging and sorting and now my grid no longer displays the data but I am not sure why as no errors are behing thrown. The code that used to work:

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

my new code attempting to add paging and sorting.

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

    }

EDIT: jqGrid Code:

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

So I ended up adding the command loadonce: true to the jqGrid config to enable client side sorting and have removed all code on the server side dealing with sorting. My grid is now displaying the data and sorting and paging fine.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM