В моем коде mvc я преобразовываю datatable в json.
Результат моего json в правильном формате, но он не заполняет мой jqgrid. РезультатОшибка привязки json jqGrid
JSON:
"{\"total\":2,\"page\":1,\"records\":2,\"rows\":[{\"id\":1,\"cell\":[\"1\",\"TEST
ACCOUNT\",\"TEST LOB\",\"TEST REPORT\"]}]}"
Код:
public string JsonForJqgrid(DataTable dt, int pageSize, int totalRecords, int page)
{
int totalPages = (int)Math.Ceiling((float)totalRecords/(float)pageSize);
StringBuilder jsonBuilder = new StringBuilder();
jsonBuilder.Append("{");
jsonBuilder.Append("\"total\":" + totalPages + ",\"page\":" + page + ",\"records\":" + (totalRecords) + ",\"rows\"");
jsonBuilder.Append(":[");
for (int i = 0; i < dt.Rows.Count; i++)
{
jsonBuilder.Append("{\"id\":" + (i+1) + ",\"cell\":[");
for (int j = 0; j < dt.Columns.Count; j++)
{
jsonBuilder.Append("\"");
jsonBuilder.Append(dt.Rows[i][j].ToString());
jsonBuilder.Append("\",");
}
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
jsonBuilder.Append("]},");
}
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
jsonBuilder.Append("]");
jsonBuilder.Append("}");
return jsonBuilder.ToString();
}
jqGrid: Действие
$("#jqTable").jqGrid({
// Ajax related configurations
url: "Staffing/LOBStaffing",
datatype: "json",
mtype: "GET",
// Specify the column names
colNames: ["ID", "Account", "Lob", "Report"],
// Configure the columns
colModel: [
{ name: "ID", index: "ID", width: 40, align: "center", key: true, hidden: true },
{ name: "Account", width: 150, align: "center" },
{ name: "Lob", width: 150, align: "center" },
{ name: "Report", width: 150, align: "center" }
],
rowNum: 10,
loadonce: true,
viewrecords: true,
sortorder: "desc",
caption: "List Staffing Details"
//gridview: true,
//scrollOffset: 0
});
Контроллер:
[HttpGet]
public JsonResult LOBStaffing()
{
string sJson = _staffing.JsonForJqgrid(_staffing.GetStaffing(), 1, 2, 1);
return Json(sJson, JsonRequestBehavior.AllowGet);
}
Лучше передать и обработать метод 'Json()' контроллера. я предполагаю, что вы получите строку json в своей сетке. –
Я фактически работаю над динамическими данными. любые предложения по некоторым работам при работе с динамическими данными? – JVELASCO
помогает ли мой ответ? –