[英]ASP.NET MVC | datatables server side filtering not working
Im new to .NET, entity framework, delegates etc. 我是.NET,实体框架,委托等的新手。
What i am trying to do is to use datatables in ASP.NET MVC5 application.. 我正在尝试做的是在ASP.NET MVC5应用程序中使用数据表。
i am following this tutorial 我正在关注本教程
http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part
http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part
its the part 1 actually. 它实际上是第1部分。 and datatables showing data fine if not filtered.
以及数据表,如果未过滤,则显示数据很好。 but when i try to filter,i get error.
但是当我尝试过滤时,我得到了错误。
Below are screenshots of errors. 以下是错误的屏幕截图。
This is a demo table for learning purpose. 这是一个用于学习目的的演示表。 It shows the categories in my database table..
它在我的数据库表中显示类别。
I have this entity class for categories 我有这个类别的实体类
public class Category
{
public virtual int CategoryID { get; set; }
public virtual int ParentCategory { get; set; }
public virtual string Name { get; set; }
[Column(TypeName = "ntext")]
public virtual string Description {get;set; }
public virtual ICollection<Product> Products { get; set; }
}
Here is the controller i am using for serving ajax purpose of datatables. 这是我用于服务数据表的ajax的控制器。
public ActionResult CategoriesList_DT(jQueryDataTableParamModel param)
{
var allCategories = _db.Categories;
IEnumerable<Category> filteredCategories;
if (!string.IsNullOrEmpty(param.sSearch))
{
//Used if particulare columns are filtered
var nameFilter = Convert.ToString(Request["sSearch_1"]);
var descriptionFilter = Convert.ToString(Request["sSearch_2"]);
//Optionally check whether the columns are searchable at all
var isNameSearchable = Convert.ToBoolean(Request["bSearchable_1"]);
var isDescriptionSearchable = Convert.ToBoolean(Request["bSearchable_2"]);
filteredCategories = _db.Categories
.Where(c => isNameSearchable && c.Name.ToLower().Contains(param.sSearch.ToLower())
||
isDescriptionSearchable && c.Description.ToLower().Contains(param.sSearch.ToLower()));
}
else
{
filteredCategories = allCategories;
}
var isNameSortable = Convert.ToBoolean(Request["bSortable_1"]);
var isDescriptionSortable = Convert.ToBoolean(Request["bSortable_2"]);
var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
Func<Category, string> orderingFunction = (c => sortColumnIndex == 1 && isNameSortable ? c.Name :
sortColumnIndex == 2 && isDescriptionSortable ? c.Description :
"");
var sortDirection = Request["sSortDir_0"]; // asc or desc
if (sortDirection == "asc")
filteredCategories = filteredCategories.OrderBy(orderingFunction);
else
filteredCategories = filteredCategories.OrderByDescending(orderingFunction);
var displayedCategories = filteredCategories.Skip(param.iDisplayStart).Take(param.iDisplayLength);
var result = from c in displayedCategories select new[] { Convert.ToString(c.CategoryID), c.Name, c.Description };
return Json(new
{
sEcho = param.sEcho,
iTotalRecords = allCategories.Count(),
iTotalDisplayRecords = filteredCategories.Count(),
aaData = result
},
JsonRequestBehavior.AllowGet);
}
and this is the js script for jquery datatables. 这是jquery数据表的js脚本。
@section Scripts {
<script type="text/javascript">
$(document).ready(function () {
var oTable = $('#datatable').dataTable({
"bServerSide": true,
"sAjaxSource": "@Url.Action("CategoriesList_DT", "Categories", new { area = "Admin" })",
"bProcessing": true,
"aoColumns": [
{
"sName": "ID",
"bSearchable": false,
"bSortable": false,
"fnRender": function (oObj) {
return '<a href=\"Company/Details/' + oObj.aData[0] + '\">View</a>';
}
},
{ "sName": "Name" },
{ "sName": "Description" }
]
});
});
</script>
}
I am not sure what error i am getting and why i am getting this error. 我不确定我要得到什么错误,以及为什么要得到这个错误。 surely i am new to this.. Also if possible plz tell me what should be the right way of implementing datatables if i am not doing it right.
当然,我是新来的。如果可能,请告诉我如果我做对的话应该是实现数据表的正确方法。
Problem fixed. 问题已解决。
It was problem with ToLower() not working with nText. 这是ToLower()无法与nText一起使用的问题。 i dnt knw why but searched net again, i guess we can use nvarchar(max) instead of nText it can also store big text data :)
我不知道为什么但又再次搜索了网络,我想我们可以使用nvarchar(max)代替nText,它也可以存储大文本数据:)
Linq to Entities : using ToLower() on NText fields Linq to Entities:在NText字段上使用ToLower()
Got little info from here too about nvarchar(max) 也从这里获得有关nvarchar(max)的少量信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.