好的,我应该在我想出来的时候发布这个,但我最终陷入了其他任务。这是我为报表实体实现的 LINQ to Entities 所做的工作。
首先,使用默认搜索加载 jqGrid 的代码很简单(一旦我弄清楚我错过了什么):
$(document).ready(function() {
// Set up jqGrid for the report search results table.
// This is displayed in a tab in the bottom section of the master page.
$("#searchResultList").jqGrid({
url: '/Report/GetResultsL2E/',
datatype: 'json',
mtype: 'GET',
colNames: ['', 'ID', 'Title', 'Post_Date', 'Start_Date', 'End_Date', 'Summary'],
colModel: [
{ name: 'act', index: 'act', width: 75, sortable: false },
{ name: 'ID', index: 'ID', width: 40, align: 'left', hidden: true },
{ name: 'Title', index: 'Title', width: 150, align: 'left' },
{ name: 'Post_Date', index: 'Post_Date', width: 80, align: 'left', formatter: 'date' },
{ name: 'Start_Date', index: 'Start_Date', width: 80, align: 'left', formatter: 'date' },
{ name: 'End_Date', index: 'End_Date', width: 80, align: 'left', formatter: 'date' },
{ name: 'Summary', index: 'Summary', width: 240, align: 'left' }
],
pager: jQuery('#searchResultPager'),
rowNum: 10,
rowList: [5, 10, 20, 50],
sortname: 'Title',
sortorder: "asc",
viewrecords: true,
imgpath: '/Scripts/jqGrid/themes/green/images',
caption: 'Report Search Results',
editurl: "/Report/Edit",
scroll: true,
height: 'auto',
recordtext: ' Reports',
pgtext: ' of ',
multiselect: true,
multiboxonly: true, //adds check box column
altRows: true,
loadComplete: function() {
var ids = jQuery("#searchResultList").getDataIDs();
for (var i = 0; i ";
se = "";
ce = "";
jQuery("#searchResultList").setRowData(ids[i], { act: be + se + ce })
}
}
}).navGrid('#searchResultPager',
{ edit: false, add: false, del: false, search: false }, //options
{height: 280, reloadAfterSubmit: false }, // edit options
{height: 280, reloadAfterSubmit: false }, // add options
{reloadAfterSubmit: false }, // del options
{} // search options
);
});
加载默认搜索集的方法返回可用报告的总集的第一页:
///
/// 查询 ReportSet 以返回一个分页、排序的报表实体属性集,以响应来自视图的调用。
///
/// 用于排序的列的名称。
/// 排序的顺序(升序或降序)。
/// 返回调用进程的页数。
/// 要为页面返回的行数。
/// 此 ActionResult 返回一个 JSON 结果,供使用 jQuery 库的 jqGrid 使用。
/// jQuery 需要一个脚本标签来链接 jQuery.js 脚本。
/// jqGrid 需要指向以下脚本和样式表的样式表链接:
///
/// jQuery/themes/base/ui.all.css
/// jqGrid/themes/green/grid.css(或其他主题CSS文件)
/// jqGrid/jquery.jqGrid.js
/// jqGrid/grid.base.js
/// jqGrid/js/jqModal.js
/// jqGrid/js/jqDnR.js
///
public ActionResult GetResultsL2E(string sidx, string sord, int page, int rows)
{
int pageIndex = Convert.ToInt32(page) - 1;
int pageSize = 行;
int totalRecords = _db.ReportSet.Count();
int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
int startRecord = pageIndex * pageSize;
列表 rowStrings = new List();
// 以固定顺序获取模型中的所有报表(用于比较)。
//var reports = from item in _db.ReportSet
// orderby item.Start_Date, item.Title
// 选择新的 { item.ID, item.Title, item.Post_Date,
// item.Start_Date, item.End_Date, item.Summary };
// 以从 jqGrid 传递的动态顺序获取模型中的所有报表。
字符串 orderBytext = "";
orderBytext = string.Format("it.{0} {1}", sidx, sord);
var 报告 = _db.ReportSet
.OrderBy(orderBytext);
列表 stringList = new List();
int 计数器 = 报告.Count();
foreach(报告中的 var 报告)
{
var 行字符串 = 新
{
id = 报告.ID,
细胞 = 新 [] {
"",
报告.ID.ToString(),
报告.标题,
报告.Post_Date.ToShortDateString(),
report.Start_Date.ToShortDateString(),
报告.End_Date.ToString(),
报告.Summary.ToString()}
};
stringList.Add(rowString);
}
var rowsOut = 新对象[计数器];
for (int i = 0; i
我后来添加了另一种方法来响应用户选择要排序的列,使用从 Albaharis 的书 C# 中讨论的 PredicateBuilder 在 Nutshell 的 Dynamically Composing Expression Predicates 部分。我在 MSDN 上 PredicateBuilder fails on nested predicates with LINQ to Entities 开始的一个问题中讨论了我的解决方案