2011-06-21 66 views
1

我正在使用asp.net mvc來創建一個網頁,呈現我天藍色表中的數據的jqGrid。網格會回調到我的控制器並使用調試器,我可以直觀地驗證數據是否正確生成。問題是,一旦數據返回一個JSON字符串時,jqGrid的引發此錯誤:jqGrid不顯示JSON數據

b.jgrid.getAccessor(p, d.cell) is undefined 
http://localhost:54758/jquery.jqGrid.min.js 
Line 65 

我在網上找了文檔,但已經在返回除了這似乎是庫快照什麼是不成功的。我已經剝離了我的代碼,只有一列數據(如下所示)作爲字符串返回,但仍然沒有。

如果我將「datatype:'local',」行添加到我的jqGrid選項中,則不會引發錯誤。但是,數據仍然沒有呈現。 「類似問題」中提出的其他問題都沒有解決我的問題。數據只是一個Id字段,是一個簡單的C#字符串。

下面是用於呈現的jqGrid ASPX文件內容:

<asp:Content ID="Content2" ContentPlaceHolderID="HeadContent" runat="server"> 
<%--Must load language tag BEFORE script tag--%> 
<script src="grid.locale-en.js" type="text/javascript"></script> 
<script src="jquery.jqGrid.min.js" type="text/javascript"></script> 

<script type="text/javascript"> 
    jQuery(document).ready(function() { 
     jQuery("#list").jqGrid({ 
      autowidth: true, 
      url: '/HouseData/GridData/', 
      datatype: 'local', <---instead of 'local' I also tried 'jsonstring' 
      mtype: 'POST', 
      colNames: ['House Name'], 
      colModel: [ 
       { name: 'houseName', index: 'houseName', key: true, width: 80, align: 'right', hidden: false }], 

      pager: jQuery('#pager'), 
      rowNum: 20, 
      rowList: [5, 10, 20, 50], 
      sortname: 'houseName', 
      sortorder: "desc", 
      height: 400, 
      viewrecords: true, 
      imgpath: '' 
     }); 
    }); 

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> 
<div id="main"> 
    <h2><a href="#">Show House Name</a></h2> 
    <div class="disignBoxFirst"> 
     <div class="boxContent"> 
       <%: Html.Partial("HouseDataSearch") %> 
     </div> 
    </div> 
    <hr /> 
    <table id="list" class="scroll" cellpadding="0" cellspacing="0"></table> 
    <div id="pager" class="scroll" style="text-align:center;"></div> 
</div> 
</asp:Content> 

用來組成JSON字符串,包含在C#文件「HouseDataController.cs」功能,低於(最初我沒有」噸具有用於上述的jqGrid中所示的「數據類型」中的aspx文件選擇):使用螢火蟲被抓住

public JsonResult GridData(string sidx, string sord, int page, int rows, string houseName) 
    {    
     //Setup values to return to the view for display of user entered values    
     ViewData["HouseName"] = houseName; 


     //Get table data 
     CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("ConnectionString"); 
     ReportStorageDataServiceContext storageContext = new ReportStorageDataServiceContext(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials); 

     int houseCount = 0; 
     IQueryable<HouseDataModel> houses = storageContext.CreateQuery<HouseDataModel>("HouseDataTable").Where(h => h.isAvailable == true); 
     List<HouseDataModel> queryDetails = new List<HouseDataModel>(); 
     foreach (HouseDataModel house in houses) 
     { 
      queryDetails.Add(house); 
      houseCount++; 
     } 

     //Figure out paging 
     int pageIndex = Convert.ToInt32(page) - 1; 
     int pageSize = rows; 
     int totalRecords = houseCount; 
     int totalPages = (int)Math.Ceiling((float)totalRecords/(float)pageSize); 

     var jsonData = new 
     { 
      total = totalPages, 
      page, 
      records = totalRecords, 
      rows = (from house in queryDetails select new { User = house.houseName }).ToArray() 
     }; 

     return Json(jsonData); 
    } 

錯誤消息。如果這不夠具體/足夠詳細,或者如果它在另一個線程中解決,請告知。

謝謝!

回答

2

試試這個:

jQuery("#list").jqGrid({ 
      autowidth: true, 
      url: '/HouseData/GridData/', 
      datatype: 'local', <---instead of 'local' I also tried 'jsonstring' 
      mtype: 'POST', 
      colNames: ['House Name'], 
      colModel: [ 
       { name: 'houseName', index: 'houseName', key: true, width: 80, align: 'right', hidden: false }], 

      pager: jQuery('#pager'), 
      rowNum: 20, 
      rowList: [5, 10, 20, 50], 
      sortname: 'houseName', 
      sortorder: "desc", 
      height: 400, 
      viewrecords: true, 
      imgpath: '', 
      jsonReader : { 
       root: "rows", 
       page: "page", 
       total: "total", 
       records: "records", 
       repeatitems: false, 
       cell: "cell", 
       id: "id", 
       userdata: "userdata",  
       },   
     }); 

我添加了jsonReader部分。我有同樣的問題,這似乎解決它。

+0

海@Matthew Rygiel謝謝。剛纔我也遇到了同樣的問題。 JsonReader爲我工作。謝謝。 – vissu

0

嘗試改變的jqGrid的MTYPE到 'GET' 和數據類型爲 'JSON':

datatype: 'json' 
mtype: 'GET' 

,改變你的GridData控制器動作的迴歸:

return Json(data, JsonRequestBehavior.AllowGet);