【发布时间】:2014-07-08 23:56:06
【问题描述】:
我正在尝试从以下地址获取data:
jQuery(document).ready(function() {
var mydata;
$.getJSON('@Url.Action("GetJsonData", "Home")', function(data) {
datatype: 'json',
mydata = data;
// alert(mydata);
});
致我的jqGrid:
$("#grid").jqGrid({
datastr: data,
datatype: 'json',
width: '100%',
colNames: ["Seq ID", "Fund ID", "Name", "Fund", "Bonus", "Allocation", "Blank", "Begin", "End"],
colModel: [
{
name: 'seqid',
index: 'seqid',
editable: true,
}, {
name: 'fund id',
index: 'fund id',
editable: true,
}, {
name: 'name',
index: 'name',
editable: true,
}, {
name: 'fund',
index: 'fund',
editable: true,
}, {
name: 'bonus',
index: 'bonus',
editable: true,
}, {
name: 'allocation',
index: 'allocation',
editable: true,
}, {
name: 'blank',
index: 'blank',
editable: true,
}, {
name: 'begin',
index: 'begin',
editable: true,
}, {
name: 'end',
index: 'end',
editable: true,
}
],
pager: '#pager',
'cellEdit': true,
'cellsubmit': 'clientArray',
editurl: 'clientArray'
});
数据如下:
{
"FUND_SEQ_ID": 1.0,
"FUND_ID": 23,
"FUND_NM": "INSTITUTIONAL",
"FUND_TICKER_NM": "TINXX",
"FUND_SALARY_IND": "A",
"FUND_BONUS_IND": "N",
"FUND_ALCTN_IND": "\u0000", <- This should be null
"BEG_DT": "20140101",
"END_DT": "24000101"
},
我试过了:datatype: jsonstring、datastr: data、data: data.. 什么都不给我,或者 p.colModel 为空或不是对象。
getJSON 方法中的数据在那里。只是不知道如何通过它。
更新:这是我在 MVC 4 Razor 中使用 DataTable 的方法。
在HomeController.cs我有一个方法:
public string GetAssociateFromDb()
{
DataTable dt = new DataTable();
string jsonData;
string connString = ConfigurationManager.ConnectionStrings["DEFCOMP"].ConnectionString;
using (SqlConnection connection = new SqlConnection())
{
connection.ConnectionString = connString;
using (var cmd = new SqlCommand("SELECT * FROM FUND", connection))
{
connection.Open();
SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
myAdapter.Fill(dt);
return JsonConvert.SerializeObject(dt); // Converted to JSON string
}
}
}
在我看来 (Index.cshtml),我在 jQGrid 的 url 中调用了该方法。
$(document).ready(function() {
jQuery("#grid").jqGrid({
url: '@Url.Action("GetAssociateFromDb", "Home")',
datatype: "json",
width: '100%',
colNames: ["Seq ID", "Fund ID", "Name", "Fund", "Salary", "Bonus", "Allocation", "Begin", "End"],
colModel: [
{ name: "FUND_SEQ_ID" },
{ name: "FUND_ID" },
{ name: "FUND_NM" },
{ name: "FUND_TICKER_NM" },
{ name: "FUND_SALARY_IND" },
{ name: "FUND_BONUS_IND" },
{ name: "FUND_ALCTN_IND" },
{ name: "BEG_DT" },
{ name: "END_DT" }
],
cmTemplate: { editable: true },
// data: JSON.parse(data), // Load Data
rowNum: 10, // Total records to show at a time by default
loadonce: true,
rowList: [10, 20], // For Paging
pager: '#pager',
jsonReader: {
repeatitems: false,
page: function () { return 1; }, // This was necessary.
root: function (obj) { return obj; },
records: function (obj) { return obj.length; }
},
viewrecords: true,
gridview: true,
autowidth: true,
height: 'auto',
hoverrows: true,
caption: "List of Funds"
});
});
【问题讨论】:
-
您在问题中包含的数据不是项目数组。您是否真的尝试以这种方式使用 jqGrid,或者您只是没有包含来自服务器的完整响应?为什么不使用
url: '@Url.Action("GetJsonData", "Home")'和datatype: "json"?这是填充网格数据的最标准方式。 -
感谢@Oleg 的评论。我确实尝试过,但网格仍然是空的。
-
数据实际上是一个数组。它看起来像提供的示例,但在
[ ]中包含了 10 个额外的记录。