【发布时间】:2016-10-01 00:30:48
【问题描述】:
请检查我的 jqgrid,它只显示空白网格,我的 json 是根据网格的预期格式。我正在使用 jqGrid 4.4.4
<script type="text/javascript">
$(function () {
$("#myGrid").jqGrid({
url: '/Home/GetData/',
datatype: "json",
contentType: "application/json; charset-utf-8",
mtype: 'GET',
colNames: ['CP', 'Val1', 'Val2', 'Val3'],
colModel: [
{ name: 'CP', index: 'CP', width: 150 },
{ name: 'Val1', index: 'Val1', width: 150 },
{ name: 'Val2', index: 'Val2', width: 150 },
{ name: 'Val3', index: 'Val3', width: 150 }
],
rowNum: 5,
rowList: [5, 10, 15],
pager: '#pager',
sortname: 'CP',
viewrecords: true,
sortorder: "asc",
viewrecords: true,
caption: "JSON Example"
});
$("#myGrid").jqGrid('navGrid', '#pager', { edit: true, add: true, del: true });
});
</script>
我的控制器方法看起来像,我正在按照 jqGrid 文档格式化数据。
[HttpGet]
public JsonResult GetData()
{
List<Rate> myList = CallProcedure<Rate>("getVal").ToList();
var jsonData = new
{
total = myList .Count,
page = 1,
records = 10,
rows = (
from d in myList
select new
{
id = d.CP,
cell = new string[] {
d.CP.ToString(),
d.Val1.ToString(),
d.Val2.ToString(),
d.Val3.ToString()
}
}).ToArray()
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
尝试在网格中设置 jsonReader 但仍然没有成功,你能指出我哪里出错了吗?提前致谢
Json 如下所示
{
"total": 1,
"page": 1,
"records": 25,
"rows": [{
"id": "AUSD",
"cell": ["AUSD ", "0.000000", "0.72315000", "0.000000"]
}, {
"id": "PPFF",
"cell": ["PPFF ", "0.000000", "1.10288000", "0.000000"]
}, {
"id": "XTYU",
"cell": ["XTYU ", "0.000000", "1.41479000", "0.000000"]
}, {
"id": "NUSD",
"cell": ["NUSD ", "-0.000020", "0.67097000", "-0.000020"]
}, {
"id": "USED",
"cell": ["USED ", "0.000000", "3.67278000", "0.000000"]
}, {
"id": "UAD",
"cell": ["UAD ", "0.000120", "1.37037000", "0.000020"]
}]
}
新的 json 看起来像,
{
"total": 1,
"page": 1,
"records": 25,
"rows": [
["Val1 ", "0.000000", "0.72315000", "0.000000"],
["Val12 ", "0.000000", "1.10288000", "0.000000"],
["Val13 ", "0.000000", "1.41479000", "0.000000"],
["Val14 ", "-0.000020", "0.67097000", "-0.000020"],
["Val15 ", "0.000000", "3.67278000", "0.000000"],
["Val16 ", "0.000120", "1.37037000", "0.000020"],
["Val17 ", "0.000000", "0.99843000", "0.000000"],
["Val18 ", "0.000000", "24.53100000", "0.000000"],
["Val19 ", "0.000000", "6.76500000", "0.000000"],
["Val23 ", "0.000000", "7.77250000", "0.000000"]
]
}
网格仍然是空白的:(
更新动作方法以返回不同格式的json,
{
"total": 25,
"page": 1,
"records": 10,
"rows": [{
"CP": "ADRR",
"Val1": "0.000000",
"Val2": "0.72315000",
"Val3": "0.000000"
}, {
"CP": "TRRT",
"Val1": "0.000000",
"Val2": "1.10288000",
"Val3": "0.000000"
}, {
"CP": "TRER",
"Val1": "0.000000",
"Val2": "1.41479000",
"Val3": "0.000000"
}]
}
json 中的不同之处在于它包含 " 代表 "CP"、"Val1" 名称。并且网格仍然是空白的
这里有什么问题?页面加载时断点就到了,它按预期返回json,
public JsonResult GetData()
{
List<Live> dd = CallProc<Live>("getLiveData").ToList();
var jsonData = new
{
total = 1,
page = 1,
records = dd.Count,
rows = (
from d in dd
select new string[]
{
CP = d.CP.ToString(),
CP1 = d.CP1.ToString(),
CP2 = d.CP2.ToString(),
Cp3 = d.Cp3.ToString()
}).ToArray()
};
string json = new JavaScriptSerializer().Serialize(jsonData); //just to check what json I am getting
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
【问题讨论】:
标签: jquery asp.net-mvc asp.net-mvc-4 jqgrid