为什么我的jqgrid没有显示任何数据?

时间:2015-03-09 18:47:33

标签: javascript c# jquery asp.net-mvc jqgrid

我想使用完整的jqgrid,但它不起作用。它没有显示任何数据,但控制器操作返回值。这是我的控制器动作代码,在我的项目中使用。我的目的是在jqgrid中使用寻呼机。请帮助我,我需要一些解决方案&在mvc中使用jqgrid的提示。

 public ActionResult itemList(jqGridViewModel jqGridParameters)
        {
            var item = from t in db.tbl_Item select t;
            var count = item.Count();
            int pageIndex = jqGridParameters.page;
            int pageSize = jqGridParameters.rows;
            int startRow = (pageIndex * pageSize) + 1;
            int totalRecords = count;
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
            var result = new
            {
                total = totalPages,
                page = pageIndex,
                records = count,
                rows = item.Select(x => new
                {
                    x.id,
                    x.itemcode,
                    x.name,
                    x.qtyLimit,
                    x.Quantity,
                    x.sellingPrice,
                    x.supplier,
                    x.unitType,
                    x.vat,
                    x.batchno,
                    x.brand,
                    x.buyingPrice,
                    x.catg
                }
                                         ).ToArray()
                       .ToPagedList(pageIndex, pageSize)
                       .Select(x => new
                       {
                           id = x.id,
                           cell = new string[] { x.id.ToString(),  
                                                            x.name,   
                                                            x.itemcode,                  
                    Convert.ToString(x.qtyLimit),
                    x.Quantity.ToString(),
                    x.sellingPrice.ToString(),
                    x.supplier,
                    x.unitType,
                    x.vat.ToString(),
                    x.batchno,
                    x.brand,
                    x.buyingPrice.ToString(),
                    x.catg 
                           }
                       }
                              ).ToArray()
            };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

我的观看代码

jQuery("#list").jqGrid({
            cache: false,
            async: false,
            url: '/Settings/itemList/',
            datatype: 'json',
            mtype: 'GET',


            colNames: ['New Item', 'Batch No', 'Supplier', 'Unit', 'B. Price', 'S. Price','Item Code','Vat','Limit'],
            colModel: [

                             { name: 'name', index: 'name', width: 110, align: 'center' },
                             { name: 'batchno', index: 'batchno', width: 110, align: 'center' },
                             { name: 'supplier', index: 'supplier', width: 110, align: 'center' },
                             { name: 'unitType', index: 'unitType', width: 110, align: 'center', editoptions: { readonly: 'readonly' } },
                             { name: 'buyingPrice', index: 'buyingPrice', width: 110, align: 'center', editoptions: { readonly: 'readonly' } },
                             { name: 'sellingPrice', index: 'sellingPrice', align: 'center' },
                             { name: 'itemcode', index: 'itemcode', width: 110, align: 'center'},
                             { name: 'vat', index: 'vat', width: 110, align: 'center', editoptions: { readonly: 'readonly' } },
                             { name: 'qtyLimit', index: 'qtyLimit', align: 'center' }

            ],

            pager: jQuery('#pager'),
            rowNum: 15,

            rowList: [5, 10, 20, 50],
            sortname: 'iid',
            sortorder: "desc",
            viewrecords: true,
            width: 960,
            height: 200,
            loadOnce: true,
            imgpath: '/scripts/themes/coffee/images',
            caption: 'Stock Information',
            jsonReader: {
                root: "Data",
                page: "CurrentPage",
                total: "TotalPages",
                records: "TotalRecords",
                repeatitems: false,
                id: "0"
            },
            recordtext: "Products {0} - {1} of {2}",
            rownumbers: true,
            pagerpos: 'center'
     });

1 个答案:

答案 0 :(得分:1)

您可以像这样定义JsonReader

jsonReader: {
                root: "Data",
                page: "CurrentPage",
                total: "TotalPages",
                records: "TotalRecords",
                repeatitems: false,
                id: "0"
            },

然后在控制器端将数据传递给具有此属性的无限对象:

var result = new
            {
                total = totalPages,
                page = pageIndex,
                records = count,
                 ...
            }

您的媒体资源名称应与您在JsonReader

中定义的相同