我在解决网格中的两个列的几个错误时遇到了问题。我创建了一个包含几个表的示例数据库,并创建了一个 jqGrid 测试应用程序来显示数据。我想对网格上的数据进行一些搜索/过滤。我使用了 Oleg 的使用 MVC 进行搜索和过滤的示例。我的前两列(ID、事件标题)运行良好,但之后的任何列都不起作用。我确定我错过了一些东西,但看不到我错过了什么。任何帮助都会很棒。下面是代码。
客户端:
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: '../Admin/GetEventsGridData/',
datatype: 'json',
mtype: 'POST',
colNames: ['ID', 'Event Title', 'Series', 'Occ', 'Department' ],
colModel: [
{ name: 'EVENT_ID', index: 'EVENT_ID', editable: true, editoptions: { readonly: 'readonly' }, width: 25, searchoptions: { sopt: ['eq', 'ne', 'lt', 'le', 'gt', 'ge']} },
{ name: 'EVENT_TITLE', index: 'EVENT_TITLE', editable: true, width: 205, searchoptions: { sopt: ['cn', 'nc', 'bw', 'bn', 'eq', 'ne', 'ew', 'en', 'lt', 'le', 'gt', 'ge']} },
{ name: 'SERIES_DESCRIPTION', index: 'SERIES_DESCRIPTION', editable: true, width: 30, searchoptions: { sopt: ['cn', 'nc', 'bw', 'bn', 'eq', 'ne', 'ew', 'en', 'lt', 'le', 'gt', 'ge']} },
{ name: 'OCCURRENCES', index: 'OCCURRENCES', editable: true, width: 20, searchoptions: { sopt: ['eq', 'ne', 'lt', 'le', 'gt', 'ge']} }
{ name: 'DEPARTMENT_NAME', index: 'DEPARTMENT_NAME', editable: true, width: 100, searchoptions: { sopt: ['cn', 'nc', 'bw', 'bn', 'eq', 'ne', 'ew', 'en', 'lt', 'le', 'gt', 'ge']} }
],
pager: '#pager',
autowidth: true,
height: 'auto',
rowNum: 10,
rowList: [5, 10, 20, 50],
sortname: 'EVENT_ID',
sortorder: "desc",
viewrecords: true,
caption: 'Events',
jsonReader: { cell: "" },
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: true, edit: true, del: true, refresh: true },
updateDialog, // add a record
updateDialog, // edit a record
updateDialog, // delete a record
{multipleSearch: true, overlay: false, width: 460 });
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(); }
});
这是我的服务器端:
using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
//using System.Data.Entity.Infrastructure;
using CATS.Models;
namespace CATS.Controllers
{
// 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
{
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;
}
}
}
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);
ObjectParameter param;
switch (propertyInfo.PropertyType.FullName)
{
case "System.Int32": // int
param = new ObjectParameter("p" + iParam, Int32.Parse(rule.data));
break;
case "System.Int64": // bigint
param = new ObjectParameter("p" + iParam, Int64.Parse(rule.data));
break;
case "System.Int16": // smallint
param = new ObjectParameter("p" + iParam, Int16.Parse(rule.data));
break;
case "System.SByte": // tinyint
param = new ObjectParameter("p" + iParam, SByte.Parse(rule.data));
break;
case "System.Single": // Edm.Single, in SQL: float
param = new ObjectParameter("p" + iParam, Single.Parse(rule.data));
break;
case "System.Double": // float(53), double precision
param = new ObjectParameter("p" + iParam, Double.Parse(rule.data));
break;
case "System.Boolean": // Edm.Boolean, in SQL: bit
param = new ObjectParameter("p" + iParam,
String.Compare(rule.data, "1", StringComparison.Ordinal) == 0 ||
String.Compare(rule.data, "yes", StringComparison.OrdinalIgnoreCase) == 0 ||
String.Compare(rule.data, "true", StringComparison.OrdinalIgnoreCase) == 0);
break;
default:
// TODO: Extend to other data types
// binary, date, datetimeoffset,
// decimal, numeric,
// money, smallmoney
// and so on
param = new ObjectParameter("p" + iParam, rule.data);
break;
}
objParams.Add(param);
}
ObjectQuery<T> filteredQuery = inputQuery.Where(sb.ToString());
foreach (var objParam in objParams)
filteredQuery.Parameters.Add(objParam);
return filteredQuery;
}
}
[HandleJsonException]
public class AdminController : Controller
{
//
// GET: /Admin/
public ActionResult Admin()
{
ViewBag.PageTitle = "Admin";
return View();
}
public ActionResult GetEventsGridData(string sidx, string sord, int page, int rows, bool _search, string filters)
{
var context = new CATEntities();
//var context = new CATContext();
//var serializer = new JavaScriptSerializer();
//var objectContext = ((IObjectContextAdapter)context).ObjectContext;
//var eventset = objectContext.CreateObjectSet<EVENT>();
Filters f = (!_search || string.IsNullOrEmpty(filters)) ? null : JsonConvert.DeserializeObject<Filters>(filters);
ObjectQuery<EVENT> filteredQuery = (f == null ? context.EVENT : f.FilterObjectSet(context.EVENT)); // (ObjectQuery<EVENT>)eventset)
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 does NOT exist in the LINQ to Entity
var queryDetails = (from item in pagedQuery
select new { item.EVENT_ID, item.EVENT_TITLE, item.EVENT_SERIES.SERIES_DESCRIPTION, item.OCCURRENCES, item.DEPARTMENT.DEPARTMENT_NAME }).ToList();
return Json(new
{
total = (totalRecords + rows - 1) / rows,
page,
records = totalRecords,
rows = (from item in queryDetails
select new[] {
item.EVENT_ID.ToString(),
item.EVENT_TITLE,
item.SERIES_DESCRIPTION,
item.OCCURRENCES.ToString()
item.DEPARTMENT_NAME
}).ToList()
});
}
public ActionResult UpdateGridData(FormCollection formCollection)
{
return Json(JsonRequestBehavior.AllowGet);
}
}
}
我的问题是我在 Occurrences 列上收到此错误:
System.Data.Entity: The argument types 'Edm.Int32' and 'Edm.String' are incompatible for this operation. Near equals expression, line 6, column 17.
Occurrences 列中的所有数据都是整数数据。这告诉我它正在寻找整数数据,但我正在向它发送字符串数据。此列与 EVENT_ID 列类似。不知道我在哪里错过了断开连接。
Series 列也有问题。在 Event 表上,Series 列是我的系列表的外键 ID。从那里我得到系列描述。部门列类似。当我尝试对这两列进行搜索/过滤时,出现此错误:
System.Data.Entity: You must specify a valid predicate for filtering the results. Parameter name: predicate
我错过了一些东西,但对于我的生活,我找不到它。