[英]Sorting DateTime Column In JQuery Datatables and ASP.NET MVC
I am using the JQuery Datatables plugin in an ASP.NET MVC project to display some tabular data, which is in turn taken from a database using Entity Framework. 我在ASP.NET MVC项目中使用JQuery Datatables插件来显示一些表格数据,这些数据又是使用实体框架从数据库中获取的。
I have configured the Datatable to use ajax search and sort routines, and I am using an extension method to allow me to specify the name of a column to be sorted as a string. 我已经将Datatable配置为使用ajax搜索和排序例程,并且正在使用扩展方法来允许我指定要作为字符串排序的列的名称。 This works perfectly for most fields, but I get an error when trying to sort a field that contains a DateTime object.
这对于大多数字段都非常适用,但是在尝试对包含DateTime对象的字段进行排序时出现错误。
View JQuery Code: 查看JQuery代码:
$('#Orders').dataTable({
"bServerSide": true,
"sAjaxSource": "/Suppliers/GetOrders/@Model.ID",
"aaSorting": [[1, 'desc']],
"bProcessing": true,
bAutoWidth: false,
"sPaginationType": "full_numbers",
"aoColumns": [
{ "bVisible": false },
{ "sName": "OrderNumber",
"bSearchable": false,
"bSortable": true,
"fnRender": function (oObj) {
return '<a href=\"/Orders/View/' + oObj.aData[0] + '\">' + oObj.aData[1] + '</a>';
}
},
{ "sName": "Supplier", "bSortable": true },
{ "sName": "Client" },
{ "sName": "OrderPlaced" },
{ "sName": "OrderReceived"},
{ "sName": "OrderedBy" },
{ "sName": "Actions",
"fnRender": function (oObj) {
return '<a href=\"/Orders/View/' + oObj.aData[0] + '\">view</a>';
}
}
]
});
Controller code: 控制器代码:
var data = DataContext.PartsOrders.Select(order =>
new {
order.SupplierID,
order.UniverseID,
order.ID,
order.OrderNumber,
Supplier = order.Supplier.Name,
Client = order.Job.Client.Name,
DateOrderPlaced = order.DateOrderPlaced,
order.DateOrderReceived,
OrderedBy = order.OrderedBy.Name
}).Where(x => x.SupplierID == id && x.UniverseID == Universe.ID);
data = data.OrderByField(columnNames[Convert.ToInt32(Request["iSortCol_0"])], Request["sSortDir_0"]);
And my ExtensionMethod is as follows, based on some code I found online. 根据在网上找到的一些代码,我的ExtensionMethod如下所示。 I'm not entirely sure I understand what this code is doing, which doesnt help me find my problem!
我不完全确定我了解这段代码在做什么,但这并没有帮助我发现问题!
public static IQueryable<T> OrderByField<T>(this IQueryable<T> q, string SortField, string SortDirection)
{
var param = Expression.Parameter(typeof(T), "p");
var prop = Expression.Property(param, SortField);
var exp = Expression.Lambda(prop, param);
string method = "";
if (SortDirection == "asc")
method = "OrderBy";
else
method = "OrderByDescending";
Type[] types = new Type[] { q.ElementType, exp.Body.Type };
var mce = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
return q.Provider.CreateQuery<T>(mce);
}
Finally, the error I get when trying to sort a column containing a date: 最后,尝试对包含日期的列进行排序时遇到的错误:
Instance property 'OrderPlaced' is not defined for type '<>f__AnonymousType6`9[System.Nullable`1[System.Int32],System.Int32,System.Int32,System.String,System.String,System.String,System.Nullable`1[System.DateTime],System.Nullable`1
this occurs in the extension method, on line var prop = Expression.Property(param, SortField);
这发生在扩展方法中,行
var prop = Expression.Property(param, SortField);
It's simply a name conflict. 这只是名称冲突。
<>f__AnonymousType6...
is the anonymous type you create in DataContext.PartsOrders.Select(...
. There is a property DateOrderPlaced
in it. Change that to OrderPlaced
, or change the OrderPlaced
column in the dataTable in your javascript to DateOrderPlaced
. <>f__AnonymousType6...
是您在DataContext.PartsOrders.Select(...
创建的匿名类型。其中有一个属性DateOrderPlaced
。将其更改为OrderPlaced
,或将javascript中dataTable中的OrderPlaced
列更改为DateOrderPlaced
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.