【发布时间】:2013-12-27 23:40:58
【问题描述】:
我一直在关注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
{} );
} );
【问题讨论】:
-
@Eagle..请显示 jqgrid 的代码..可能有错误
-
@Avinash 很抱歉,因为匆忙出去工作而忘记了。我现在已经添加了。
标签: c# jqgrid asp.net-mvc-5