2012-06-26 52 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