2012-06-26 51 views
0

我已经尽力了,但我没有得到任何成功。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); 

     } 

我的看法是

<table id="jqgProducts" cellpadding="0" cellspacing="0"></table> 
<div id="jqgpProducts" style="text-align:center;"></div> 


<script src="@Url.Content("~/Scripts/jquery.jqGrid.min.js")" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(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%' 
     }); 
    }); 
    </script> 

我得到JSON结果在我的控制器的POST方法,但该数据没有得到绑定到我的网格..所有我看到的是一个空的格子,当我尝试结合一些地方的数据,它的工作好,能有一个人,请帮助我在此

+0

是你在同一个域的JSON结果网址是什么?如果直接点击它,你会看到正确的JSON输出吗? –

+0

是的JSON出来放有...它在同一个域中 –

回答

1

您必须使用必要的参数以正确的格式返回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",  
       },  

+0

以及它的工作后,我加入这部分jsonReader:{ 根:“行”, 页:“页”, 总:“总”, 记录: “记录”, repeatitems:假的, 细胞: “细胞”, ID: “ID”, 用户数据: “用户数据”, }, –

+0

我编辑乌尔答,这样我可以把它标记为ANS –

+0

我批准了你的修改。感谢您将它标记为答案。 – VJAI