【发布时间】:2013-12-09 17:15:41
【问题描述】:
无法绑定我的 JQgrid,下面是我的代码:
尝试使用 Json 通过控制器将一些数据绑定到 jqgrid,但无法绑定网格。在下面附上我的代码。
Controller Class:
public class TransactionTypeController : Controller
{
//
// GET: /TransactionType/
public ActionResult Index(EMM_New.Models.TransactionTypeModal TransactionType)
{
List<EMM_New.Models.TransactionTypeEntity> lst = TransactionType.GetTransactionType();
ViewBag.TransactionTypeData = lst;
return View();
}
public ActionResult GetGridData(string sidx, string sord, int page, int rows)
{
return Content(JsonHelper.JsonForJqgrid(GetDataTable(sidx, sord, page, rows), rows, GetTotalCount(), page), "application/json");
}
public DataTable GetDataTable(string sidx, string sord, int page, int pageSize)
{
int startIndex = (page - 1) * pageSize;
int endIndex = page * pageSize;
DataTable table = new DataTable();
table.Columns.Add("CustomerID", typeof(int));
table.Columns.Add("ContactName", typeof(string));
table.Columns.Add("Address", typeof(string));
table.Columns.Add("City", typeof(string));
table.Columns.Add("PostalCode", typeof(string));
//
// Here we add five DataRows.
//
table.Rows.Add(25, "Ramesh", "Combivent", "Bangalore", "463456");
table.Rows.Add(50, "Sam", "Enebrel", "Hosur", "463456");
table.Rows.Add(10, "Christoff", "Hydralazine", "Bangalore", "463456");
table.Rows.Add(21, "Janet", "Combivent", "Hosur", "463456");
table.Rows.Add(100, "Melanie", "Dilantin", "Bangalore", "463456");
return table;
}
public int GetTotalCount() {
return 10;
}
}
JsonHelper 类:
public class JsonHelper
{
public static 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("{\"i\":" + (i) + ",\"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();
}
}
查看:
<table id="list" class="scroll" >
</table>
<div id="pager" class="scroll" style="text-align: center;">
</div>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#list").jqGrid({
url: '/TransactionType/GetGridData/',
datatHomeype: 'json',
mtype: 'GET',
colNames: ['Customer ID', 'Contact Name', 'Address', 'City', 'Postal Code'],
colModel: [
{ name: 'CustomerID', index: 'CustomerID', width: 100, align: 'left' },
{ name: 'ContactName', index: 'ContactName', width: 150, align: 'left' },
{ name: 'Address', index: 'Address', width: 300, align: 'left' },
{ name: 'City', index: 'City', width: 150, align: 'left' },
{ name: 'PostalCode', index: 'PostalCode', width: 100, align: 'left' }
],
pager: jQuery('#pager'),
rowNum: 10,
rowList: [5, 10, 20, 50],
sortname: 'CustomerID',
sortorder: "asc",
viewrecords: true,
caption: 'JqGrid from class Binding'
}).navGrid(pager, { edit: false, add: false, del: false, refresh: true, search: true });
});
</script>
能够看到 Jqgrid,但没有填充数据....我在做什么磨损。请帮助我解决这个问题..
【问题讨论】:
-
/TransactionType/GetGridData/有没有返回json数据?
-
是的,有一些数据返回.....
-
介意显示数据吗?可能是json数据格式不对。
-
{"total":1,"page":1,"records":10,"rows":[{"CustomerID":"25","ContactName":"Ramesh","地址":"Combivent","City":"Bangalore","PostalCode":"463456"},{"CustomerID":"50","ContactName":"Sam","Address":"Enebrel"," City":"Hosur","PostalCode":"463456"},{"CustomerID":"10","ContactName":"Christoff","Address":"Hydralazine","City":"Bangalore"," PostalCode":"463456"},{"CustomerID":"21","ContactName":"Janet","Address":"Combivent","City":"Hosur","PostalCode":"463456"}, {"CustomerID":"100","ContactName":"Melanie","Address":"Dilantin","City":"Bangalore","PostalCode":"463456"}]}
-
在这里尝试了我的输出 Json 文本,无论它是否有效。它说它的有效json:jsonlint.com