我一直在关注https://tpeczek.codeplex.com/的教程以使jqGrid正常工作,并在更新我的GetData()actionresult以启用分页和排序之后,现在我的网格不再显示数据,但我不确定为什么没有错误被抛出。以前的代码:
public ActionResult GetData()
{
try
{
var model = (from s in db.Sections
select new
{
s.ID,
s.RouteName,
s.Title
}).ToList();
return Json(model, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
ErrorSignal.FromCurrentContext().Raise(ex);
return Json(null, JsonRequestBehavior.AllowGet);
}
}
我的新代码试图添加分页和排序。
public ActionResult GetData(string sidx,string sord,int page,int rows) { 尝试 { int RowCount = db.Sections.Count(); int SkipCount =(page * rows);
string OrderBy = (sidx + " " + sord);
var SectionData = new
{
total = (int)Math.Ceiling((float)RowCount / (float)rows),
page = page,
records = RowCount,
rows = (from s in db.Sections
select new
{
id = s.ID,
cell = new string[] {
SqlFunctions.StringConvert((double)s.ID).Trim(),
s.RouteName,
s.Title
}
.OrderBy(x => sidx)
.Skip(SkipCount)
.Take(rows)
}).ToArray()
};
return Json(SectionData, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
ErrorSignal.FromCurrentContext().Raise(ex);
return Json(null, JsonRequestBehavior.AllowGet);
}
}
编辑: jqGrid代码:
<script type="text/javascript">
$( document ).ready( function ()
{
$( '#Sections' ).jqGrid( {
url: '/Admin/Section/GetData',
datatype: 'json',
mtype: 'GET',
colNames: ['ID', 'RouteName', 'Title'],
colModel: [
{ name: 'ID', index: 'ID', width: '10' },
{ name: 'RouteName', index: 'RouteName', width: '50' },
{ name: 'Title', index: 'Title' }
],
autowidth: true,
height: '100%',
pager: $( '#SectionsPager' ),
rowNum: 10,
sortname: 'ID',
sortorder: 'asc',
viewrecords: true
} ).navGrid(
'#SectionsPager',
//enabling buttons
{ add: true, del: false, edit: false, search: false },
//edit options
{ width: 'auto' },
//add options
{ width: 'auto', url: '/Admin/Section/Add' },
//delete options
{} );
} );
答案 0 :(得分:0)
所以我最终将命令loadonce: true
添加到jqGrid配置中以启用客户端排序,并删除了服务器端处理排序的所有代码。我的网格现在显示数据,排序和分页正常。
答案 1 :(得分:-1)
您需要设置:datatype: "json"
,如果您正在使用loadOnce:true
删除..