JQGrid没有显示json数据

我已经努力了,但我没有取得任何成功。

我的控制器是

 public ActionResult CompOff() { return View(); } [HttpPost] public JsonResult CompOff(RegisterCompOff r) { var compoffs = db.RegisterCompOffs.Where(l => l.Employee.Id == this.EmployeeId).Select(l => new { l.CompOffDate, l.Description, l.ExpiryDate, l.IsApproved, l.IsUtilized }).ToList(); return Json(compoffs); } 

我的观点是

 
$(document).ready(function () { $('#jqgProducts').jqGrid({ //url from wich data should be requested url: '@Url.Action("CompOff")', //type of data datatype: 'json', //url access method type mtype: 'POST', //columns names colNames: ['CompOffDate', 'Description', 'ExpiryDate', 'IsApproved', 'IsUtilized'], //columns model colModel: [ { name: 'CompOffDate', index: 'CompOffDate', align: 'left' }, { name: 'Description', index: 'Description', align: 'left' }, { name: 'ExpiryDate', index: 'ExpiryDate', align: 'left' }, { name: 'IsApproved', index: 'IsApproved', align: 'left' }, { name: 'IsUtilized', index: 'IsUtilized', align: 'left' } ], //pager for grid pager: $('#jqgpProducts'), //number of rows per page rowNum: 10, //initial sorting column sortname: 'CompOffDate', //initial sorting direction sortorder: 'asc', //we want to display total records count viewrecords: true, //grid height height: '100%' }); });

我得到了json结果我的控制器的post方法,但是数据没有被绑定到我的网格…我看到的是一个空网格,当我尝试绑定一些本地数据时,它运行良好,可以有一个请帮帮我

您必须使用必要的参数以适当的格式返回JSON数据。

对于前者

 return Json(new{ total = 1, page = 1, records = collection.Count, // actual data count rows = collection // actual data }); 

您没有返回totalpage (当前页面), recordsrows

试试这个..

 [HttpPost] public JsonResult CompOff(RegisterCompOff r) { var compoffs = db.RegisterCompOffs.Where(l => l.Employee.Id == this.EmployeeId).Select(l => new { l.CompOffDate, l.Description, l.ExpiryDate, l.IsApproved, l.IsUtilized }).ToList(); return Json(new{ total = 100, // change into actual value page = 1, //first page records = compoffs.Count(), // no. of records you are returning now rows = compoffs // data }); } 

并添加以下部分进行查看

 jsonReader : { root: "rows", page: "page", total: "total", records: "records", repeatitems: false, cell: "cell", id: "id", userdata: "userdata", },