2012-05-31 50 views
0

我想在jqGrid的(4.3.2),顯示行沒有帶得到了成功呢。我的web服務的反應是:的jqGrid不顯示行

{"d": 
{"__type":"JSONResponse:pe", 
"ActionType":null, 
"JSONData":" 
      {\"total\":1, 
      \"page\":1, 
      \"records\":5, 
      \"rows\":[ 
         {\"invid\":1,\"cell\":[{\"invid\":1,\"amount\":500,\"tax\":65,\"total\":565,\"note\":\"myNote 0\"}]}, 
         {\"invid\":2,\"cell\":[{\"invid\":2,\"amount\":510,\"tax\":75,\"total\":585,\"note\":\"myNote 1\"}]}, 
         {\"invid\":3,\"cell\":[{\"invid\":3,\"amount\":520,\"tax\":85,\"total\":605,\"note\":\"myNote 2\"}]}, 
         {\"invid\":4,\"cell\":[{\"invid\":4,\"amount\":530,\"tax\":95,\"total\":625,\"note\":\"myNote 3\"}]}, 
         {\"invid\":5,\"cell\":[{\"invid\":5,\"amount\":540,\"tax\":105,\"total\":645,\"note\":\"myNote 4\"}]} 
         ] 
      }", 
"Status":false 
} 

} < - (假設在上面的代碼中這個大括號)

我的網格配置是這樣的: -

<script type="text/javascript"> 
    $(document).ready(function() { 

     gridConfig(); 

    }); 

    function gridConfig() { 
     var request = JSON.stringify(requestData()); 
     var jsonResponse; 

     $("#list").jqGrid({ 
      url: '/ScriptServices/JsonService.svc/Process', 
      ajaxGridOptions: { contentType: 'application/json; charset=utf-8' }, 
      datatype: 'json', 
      postData: { request: function() { return request; } }, 
      mtype: 'GET', 
      colNames: ['Inv No', 'Amount', 'Tax', 'Total', 'Notes'], 
      colModel: [ 
        { name: 'invid', index: 'invid', width: 55 }, 
        { name: 'amount', index: 'amount', width: 80, align: 'right' }, 
        { name: 'tax', index: 'tax', width: 80, align: 'right' }, 
        { name: 'total', index: 'total', width: 80, align: 'right' }, 
        { name: 'note', index: 'note', width: 150, sortable: false } 
        ], 
      pager: $('#pager'), 
      rowNum: 10, 
      rowList: [10, 20, 30], 
      sortname: 'invid', 
      sortorder: 'desc', 
      viewrecords: true, 
      jsonReader: { 
       root: "d.JSONData.rows", 
       page: "d.JSONData.page", 
       total: "d.JSONData.total", 
       records: "d.JSONData.records", 
       cell: "cell", 
       id:0 
      }, 
      caption: 'My first grid' 
     }); 

    }; 

    function requestData() { 
     var request; 
     request = { 
      ActionType: 'GET_GRID_DATA', 
      RequestJSONData: '{ }' 
     }; 
     return request; 
    } 

</script> 

我無法找到問題。是否需要其他格式。幫助請..

UPDATE1:

現在我的數據是奧列格建議經過適當JSON格式的到來,而是因爲在我的JSON數據__type額外領域,其無法正常渲染。

改性JSON數據看起來像: -

{ 
"d": 
    {"__type":"JSONResponse:pe", 
    "ActionType":"GRID_RESP", 
    "JSONData": 
       {"__type":"JQGridJSONReader:pe", 
       "total":1, 
       "page":1, 
       "records":5, 
       "rows":[ 
         {"__type":"InvoiceGrid:pe", 
         "invid":1, 
         "cell":[{"__type":"JqGridCell:pe","invid":1,"amount":500,"tax":65,"total":565,"note":"myNote 0"}]}, 
         .... and More Rows 
         ] 
       }, 
    "Status":true 
} 

} < - (假設在上面的代碼本的大括號)

我已經檢查這個link和實現的默認構造建議,protected internal是不可能在我的情況。

我的服務的配置是這樣的:

<system.serviceModel> 
    <services> 
     <service name="JsonService" behaviorConfiguration="JsonServiceAspNetAjaxBehavior"> 
      <endpoint address="" behaviorConfiguration="JsonServiceEndPointBehavior" 
       binding="webHttpBinding" contract="JsonService" /> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
      <behavior name="JsonServiceEndPointBehavior"> 
       <enableWebScript/> 
       <dataContractSerializer ignoreExtensionDataObject="true" /> 
      </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
      <behavior name="JsonServiceAspNetAjaxBehavior"> 
       <serviceMetadata httpGetEnabled="true"/> 
       <serviceDebug includeExceptionDetailInFaults="true" /> 
      </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
     multipleSiteBindingsEnabled="true" /> 
</system.serviceModel> 

如果我申請到<webHttp/>endpointBehaviors得到「變量UriTemplate查詢的值必須具有可以通過‘QueryStringConverter’轉換類型」的錯誤。

我的web服務是這樣的: -

[ServiceContract(Namespace = "http://www.abhishek.com/portal/json")] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class JsonService 
{ 
    [OperationContract] 
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] 
    [GenerateScriptType(typeof(JsonService), ScriptTypeId = "JsonService1")] 
    public PresentationEntities.JSONResponse Process(PresentationEntities.JSONRequest request) 
    { 
     JSONResponse response; 

     try 
     { 
      if (request == null) 
      { 
       throw new ArgumentNullException("request", string.Format(ExceptionHelper.ARG_NULL_EXP_MSG, "JSONManager")); 
      } 
      else 
      { 
       JQGridTransalator gridtransalator = new JQGridTransalator(); 
       response = gridtransalator.JQGridToJSONResponse(); // fill response.JSONData with JQGridJSONReader 
      } 
     } 
     catch (Exception) 
     { 
      throw; 
     } 

     return response; 
    }} 

JQGridJSONReader是這樣的: -

[DataContract(Name = "JQGridJSONReader", Namespace = "pe")] 
public class JQGridJSONReader 
{ 
    public JQGridJSONReader() 
    { 

    } 

    [DataMember(Name = "total", Order = 1)] 
    public int Total { get; set; } 

    [DataMember(Name = "page", Order = 2)] 
    public int Page { get; set; } 

    [DataMember(Name = "records", Order = 3)] 
    public int Records { get; set; } 

    [DataMember(Name = "rows", Order = 4)] 
    public List<InvoiceGrid> Rows { get; set; } 
} 

任何建議從JSON數據刪除__type。

UPDATE2:

更新的代碼,這樣我可以更清楚

[DataContract(Name = "InvoiceGrid", Namespace = "pe")] 
public class InvoiceGrid 
{ 
    [DataMember(Name = "cell", Order = 2)] 
    public List<JqGridCell> Cell { get; set; } 
} 

[DataContract(Name = "JqGridCell", Namespace = "pe")] 
public class JqGridCell 
{ 
    [DataMember(Name = "invid", Order = 1)] 
    public int Invid { get; set; } 

    [DataMember(Name = "invdate", Order = 2)] 
    public DateTime InvDate { get; set; } 

    [DataMember(Name = "amount", Order = 3)] 
    public double Amount { get; set; } 

    [DataMember(Name = "tax", Order = 4)] 
    public double Tax { get; set; } 

    [DataMember(Name = "total", Order = 5)] 
    public double Total { get; set; } 

    [DataMember(Name = "note", Order = 6)] 
    public string Note { get; set; } 
} 

回答

1

你的錯誤回答我的問題是,你用序列化數據d.JSONData從對象到JSON 兩次對象。也許你使用了一些JSON序列手動,而不是使用對象,並讓.NET序列化數據本身。

怎麼可以看到像d.JSONData.rows數據無法訪問,因爲d.JSONData而不必rows屬性對象。

+0

我糾正了我的json數據,現在它的好來臨。但__type是額外的領域,我與json數據相互影響我的jsonReader格式。請參閱** Update1 ** – abhishekrvce

+0

我收到了錯誤消息。誤差在' 「小區」:[{ 「__類型」: 「JqGridCell:PE」, 「因維」:1, 「量」:500, 「稅」:65, 「總」:565, 「注意」:「myNote 0" }]},'。很快我會發布答案。感謝@ Oleg給我提示如何將.net對象轉換爲json對象。 – abhishekrvce

+0

是的,我手動使用JSON序列化。糾正了這一點。可能在將來發送響應時,我永遠不會那樣做,因爲ASP.NET已經把它給我。 :) – abhishekrvce

0

糾正的第一個錯誤: - 正如@Oleg所建議的,我將JSONData(以字符串格式)轉換爲json對象。這是由於在發送數據之前使用了JSON序列化。校正

第二錯誤: -

我的細胞類型是這樣的:List<GridData>和的GridData有細胞含量(這是複雜的類型,即GridData)。所以我得到這個

"cell":[{"__type":"JqGridCell:pe","invid":1,"amount":500,"tax":65,"total":565,"n‌​ote":"myNote 0"}]},在這個單元格有一個複雜類型作爲第一行。想像像"cell":[{ComplexType}]所以我得到了「[對象對象]」在我的第一個單元格中,其他單元格保持空白。

因此更正的單元格類型爲List<string>向其添加了網格數據,然後將單元格添加到List<InvoiceGrid>

 JQGridJSONReader input = new JQGridJSONReader(); 
     input.Page = 1; 
     input.Total = 1; 
     input.Records = 5; 

     input.Rows = new List<InvoiceGrid>(); 


     for (int i = 0; i < 5; i++) 
     { 
      InvoiceGrid invGrid = new InvoiceGrid(); 
      invGrid.Cell = new List<string>(); 
      JqGridCell gridrows = new JqGridCell(); 
      gridrows.Invid = 1 + i; 
      gridrows.Amount = 500.00 + (i * 10); 
      gridrows.Tax = 65.00 + (i * 10); 
      gridrows.Total = gridrows.Amount + gridrows.Tax; 
      gridrows.Note = "myNote " + i.ToString(); 
      invGrid.Cell.Add(gridrows.Invid.ToString()); 
      invGrid.Cell.Add(gridrows.InvDate.ToString()); 
      invGrid.Cell.Add(gridrows.Amount.ToString()); 
      invGrid.Cell.Add(gridrows.Tax.ToString()); 
      invGrid.Cell.Add(gridrows.Total.ToString()); 
      invGrid.Cell.Add(gridrows.Note.ToString()); 
      input.Rows.Add(invGrid); 
     } 

我的想法是錯誤的,因爲__type作爲額外的JSON數據。但似乎jqGrid忽略了這一點。

我的json數據現在看起來像這樣。 注意: - 我這次忽略了邀請,但可以包含在內。

{"d": 
{"__type":"JSONResponse:pe", 
"ActionType":"GRID_RESP", 
"JSONData": 
      {"__type":"JQGridJSONReader:pe", 
      "total":1, 
      "page":1, 
      "records":5, 
      "rows":[ 
        {"__type":"InvoiceGrid:pe", 
        "cell":["1","500","65","565","myNote 0"]}, 
        {"__type":"InvoiceGrid:pe", 
        "cell":["2","510","75","585","myNote 1"]}, 
        {"__type":"InvoiceGrid:pe", 
        "cell":["3","520","85","605","myNote 2"]}, 
        {"__type":"InvoiceGrid:pe", 
        "cell":["4","530","95","625","myNote 3"]}, 
        {"__type":"InvoiceGrid:pe", 
        "cell":["5","540","105","645","myNote 4"]} 
        ] 
      }, 
"Status":true 
}} 

非常感謝@Oleg。