我正在尝试在 jqGrid (4.3.2) 中显示行但还没有成功。我的网络服务响应是:
{"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>
我无法找到问题。是否需要任何其他格式。请帮忙..
更新1:
现在我的数据在 Oleg 建议后以正确的 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
}
} <- (假设上面代码中的这个花括号)
我已经检查了这个链接并实现了默认的构造函数建议,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' 转换的类型”错误。
我的网络服务看起来像这样:-
[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 的任何建议。
更新2:
更新代码以便我可以更清楚地回答我的问题
[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; }
}