可能您在服务器端有问题。您能否将您的问题附加到您当前使用的 DynamicGridData 操作代码中。该操作应以filters 作为参数。
您当前代码的某些部分肯定是错误的。例如jqGrid 是 jQuery 插件。因此,jQuery 的方法将使用您用作jQuery("#list").jqGrid(...); 的主要jqGrid 方法进行扩展。所以在jqGrid初始化之后jQuery("#list").jqGrid会是一个函数。在您的代码(最后一条语句)中,您用对象{ search: { ... } } 覆盖jQuery("#list").jqGrid 方法。你应该做的是
jQuery.extend(jQuery.jgrid.search, {
odata : ['equal', 'not equal','contains']
});
例如here 描述了如何覆盖emptyrecords 默认值。您不需要在默认 jqGrid 设置中包含已经相同的值。
此外,如果您在所有可搜索列上使用 searchoptions: { sopt: ['eq', 'ne', 'cn']},则无需进行更改。
在您的问题文本中,您没有解释您想要做什么。您当前的代码是让您在初始网格加载时使用等于true 的过滤器Message。奇怪的是网格中没有名称为Message 的列。如果您只想向服务器发送一些附加信息,您最好使用postData 参数:
postData: {Message:true}
我继续建议您从 jqGrid 定义中删除垃圾,例如 jqGrid 和 sortable: true, search: true, sorttype: 'text', autoFit: true, stype:'text', align: 'left' 的 imgpath 和 multipleSearch 参数,它们是未知的或默认的。
更新:the Phil Haack demo 的原始代码很老,它使用 LINQ to SQL。就像我之前写的(参见here)实体框架(EF)允许使用排序、分页和过滤/搜索,而无需任何附加组件,如System.Linq.Dynamic 形式的LINQ 动态查询库。所以我给你做了一个演示,将the Phil Haack demo修改为EF。
因为你使用的是旧版本的 Visual Studio(VS2008 和 ASP.NET MVC 2.0)我也在 VS2008 中做了演示。
你可以从here和VS2010演示here下载我的VS2008演示。
在我展示的代码中(除了在 ASP.NET MVC 2.0 中使用高级搜索和工具栏搜索)如何以 JSON 格式从 ASP.NET MVC 返回异常信息以及如何使用loadError 捕获信息方法并显示相应的错误信息。
为了从 ObjectQuery 表示的 EF 对象构造 Where 语句,我定义了以下帮助程序类:
public class Filters {
public enum GroupOp {
AND,
OR
}
public enum Operations {
eq, // "equal"
ne, // "not equal"
lt, // "less"
le, // "less or equal"
gt, // "greater"
ge, // "greater or equal"
bw, // "begins with"
bn, // "does not begin with"
//in, // "in"
//ni, // "not in"
ew, // "ends with"
en, // "does not end with"
cn, // "contains"
nc // "does not contain"
}
public class Rule {
public string field { get; set; }
public Operations op { get; set; }
public string data { get; set; }
}
public GroupOp groupOp { get; set; }
public List<Rule> rules { get; set; }
private static readonly string[] FormatMapping = {
"(it.{0} = @p{1})", // "eq" - equal
"(it.{0} <> @p{1})", // "ne" - not equal
"(it.{0} < @p{1})", // "lt" - less than
"(it.{0} <= @p{1})", // "le" - less than or equal to
"(it.{0} > @p{1})", // "gt" - greater than
"(it.{0} >= @p{1})", // "ge" - greater than or equal to
"(it.{0} LIKE (@p{1}+'%'))", // "bw" - begins with
"(it.{0} NOT LIKE (@p{1}+'%'))", // "bn" - does not begin with
"(it.{0} LIKE ('%'+@p{1}))", // "ew" - ends with
"(it.{0} NOT LIKE ('%'+@p{1}))", // "en" - does not end with
"(it.{0} LIKE ('%'+@p{1}+'%'))", // "cn" - contains
"(it.{0} NOT LIKE ('%'+@p{1}+'%'))" //" nc" - does not contain
};
internal ObjectQuery<T> FilterObjectSet<T> (ObjectQuery<T> inputQuery) where T : class {
if (rules.Count <= 0)
return inputQuery;
var sb = new StringBuilder();
var objParams = new List<ObjectParameter>(rules.Count);
foreach (Rule rule in rules) {
PropertyInfo propertyInfo = typeof (T).GetProperty (rule.field);
if (propertyInfo == null)
continue; // skip wrong entries
if (sb.Length != 0)
sb.Append(groupOp);
var iParam = objParams.Count;
sb.AppendFormat(FormatMapping[(int)rule.op], rule.field, iParam);
// TODO: Extend to other data types
objParams.Add(String.Compare(propertyInfo.PropertyType.FullName,
"System.Int32", StringComparison.Ordinal) == 0
? new ObjectParameter("p" + iParam, Int32.Parse(rule.data))
: new ObjectParameter("p" + iParam, rule.data));
}
ObjectQuery<T> filteredQuery = inputQuery.Where (sb.ToString ());
foreach (var objParam in objParams)
filteredQuery.Parameters.Add (objParam);
return filteredQuery;
}
}
在示例中,我仅使用两种数据类型 integer (Edm.Int32) 和 string (Edm.String)。您可以根据propertyInfo.PropertyType.FullName 值轻松扩展示例以使用更多类型。
向 jqGrid 提供数据的控制器操作非常简单:
public JsonResult DynamicGridData(string sidx, string sord, int page, int rows, bool _search, string filters)
{
var context = new HaackOverflowEntities();
var serializer = new JavaScriptSerializer();
Filters f = (!_search || string.IsNullOrEmpty (filters)) ? null : serializer.Deserialize<Filters> (filters);
ObjectQuery<Question> filteredQuery =
(f == null ? context.Questions : f.FilterObjectSet (context.Questions));
filteredQuery.MergeOption = MergeOption.NoTracking; // we don't want to update the data
var totalRecords = filteredQuery.Count();
var pagedQuery = filteredQuery.Skip ("it." + sidx + " " + sord, "@skip",
new ObjectParameter ("skip", (page - 1) * rows))
.Top ("@limit", new ObjectParameter ("limit", rows));
// to be able to use ToString() below which is NOT exist in the LINQ to Entity
var queryDetails = (from item in pagedQuery
select new { item.Id, item.Votes, item.Title }).ToList();
return Json(new {
total = (totalRecords + rows - 1) / rows,
page,
records = totalRecords,
rows = (from item in queryDetails
select new[] {
item.Id.ToString(),
item.Votes.ToString(),
item.Title
}).ToList()
});
}
为了以 JSON 形式将异常信息发送到 jqGrid,我将控制器的标准 [HandleError] 属性 (HomeController) 替换为 [HandleJsonException],我将其定义如下:
// to send exceptions as json we define [HandleJsonException] attribute
public class ExceptionInformation {
public string Message { get; set; }
public string Source { get; set; }
public string StackTrace { get; set; }
}
public class HandleJsonExceptionAttribute : ActionFilterAttribute {
// next class example are modification of the example from
// the http://www.dotnetcurry.com/ShowArticle.aspx?ID=496
public override void OnActionExecuted(ActionExecutedContext filterContext) {
if (filterContext.HttpContext.Request.IsAjaxRequest() && filterContext.Exception != null) {
filterContext.HttpContext.Response.StatusCode =
(int)System.Net.HttpStatusCode.InternalServerError;
var exInfo = new List<ExceptionInformation>();
for (Exception ex = filterContext.Exception; ex != null; ex = ex.InnerException) {
PropertyInfo propertyInfo = ex.GetType().GetProperty ("ErrorCode");
exInfo.Add(new ExceptionInformation() {
Message = ex.Message,
Source = ex.Source,
StackTrace = ex.StackTrace
});
}
filterContext.Result = new JsonResult() {Data=exInfo};
filterContext.ExceptionHandled = true;
}
}
}
在客户端,我使用了以下 JavaScript 代码:
var myGrid = $('#list'),
decodeErrorMessage = function(jqXHR, textStatus, errorThrown) {
var html, errorInfo, i, errorText = textStatus + '\n' + errorThrown;
if (jqXHR.responseText.charAt(0) === '[') {
try {
errorInfo = $.parseJSON(jqXHR.responseText);
errorText = "";
for (i=0; i<errorInfo.length; i++) {
if (errorText.length !== 0) {
errorText += "<hr/>";
}
errorText += errorInfo[i].Source + ": " + errorInfo[i].Message;
}
}
catch (e) { }
} else {
html = /<body.*?>([\s\S]*)<\/body>/.exec(jqXHR.responseText);
if (html !== null && html.length > 1) {
errorText = html[1];
}
}
return errorText;
};
myGrid.jqGrid({
url: '<%= Url.Action("DynamicGridData") %>',
datatype: 'json',
mtype: 'POST',
colNames: ['Id', 'Votes', 'Title'],
colModel: [
{ name: 'Id', index: 'Id', key: true, width: 40,
searchoptions: { sopt: ['eq', 'ne', 'lt', 'le', 'gt', 'ge'] }
},
{ name: 'Votes', index: 'Votes', width: 40,
searchoptions: { sopt: ['eq', 'ne', 'lt', 'le', 'gt', 'ge'] }
},
{ name: 'Title', index: 'Title', width: 400,
searchoptions: { sopt: ['cn', 'nc', 'bw', 'bn', 'eq', 'ne', 'ew', 'en', 'lt', 'le', 'gt', 'ge'] }
}
],
pager: '#pager',
rowNum: 10,
rowList: [5, 10, 20, 50],
sortname: 'Id',
sortorder: 'desc',
rownumbers: true,
viewrecords: true,
altRows: true,
altclass: 'myAltRowClass',
height: '100%',
jsonReader: { cell: "" },
caption: 'My first grid',
loadError: function(jqXHR, textStatus, errorThrown) {
// remove error div if exist
$('#' + this.id + '_err').remove();
// insert div with the error description before the grid
myGrid.closest('div.ui-jqgrid').before(
'<div id="' + this.id + '_err" style="max-width:'+this.style.width+
';"><div class="ui-state-error ui-corner-all" style="padding:0.7em;float:left;"><span class="ui-icon ui-icon-alert" style="float:left; margin-right: .3em;"></span><span style="clear:left">' +
decodeErrorMessage(jqXHR, textStatus, errorThrown) + '</span></div><div style="clear:left"/></div>')
},
loadComplete: function() {
// remove error div if exist
$('#' + this.id + '_err').remove();
}
});
myGrid.jqGrid('navGrid', '#pager', { add: false, edit: false, del: false },
{}, {}, {}, { multipleSearch: true, overlay: false });
myGrid.jqGrid('filterToolbar', { stringResult: true, searchOnEnter: true, defaultSearch: 'cn' });
myGrid.jqGrid('navButtonAdd', '#pager',
{ caption: "Filter", title: "Toggle Searching Toolbar",
buttonicon: 'ui-icon-pin-s',
onClickButton: function() { myGrid[0].toggleToolbar(); }
});
结果,如果在搜索工具栏中键入任何非数字文本(如“ttt”),则会收到控制器操作代码异常(在Int32.Parse(rule.data) 中)。客户端之一将看到以下消息:
我从控制器向 jqgrid 发送有关所有内部异常的信息。因此,例如,连接到 SQL 服务器的错误看起来像
在现实世界中,验证用户输入并使用面向应用程序的错误消息引发异常。我在demo中特别使用了没有这样的验证来显示所有类型的异常都会被jqGrid缓存和显示。
更新 2:在 the answer 中,您将找到修改后的 VS2010 演示(可从 here 下载),它演示了 jQuery UI 自动完成的用法。 Another answer 进一步扩展代码以导出 Excel 格式的网格。