如何在ASP.net Core中实现dataTables服务器端分页/搜索/排序 [英] How to implement dataTables Server side Side Paging/Searching/Sorting in ASP.net Core
问题描述
我正在尝试从jQuery Datatable的服务器端执行搜索,排序和分页, 按照代码,但是,我无法获得用于排序的参数,即从datatble搜索到我的MVC Controller. 这是我的代码.
I am trying to perform Searching, Sorting, Paging from server-side of jQuery Datatable, I have written Following Code but, i cannot get parameters for sorting, Searching From datatble to my MVC Controller. Here is my code.
我的数据表看起来像...
My datatable Looks Like...
<script>
$(document).ready(function () {
$("#newT").DataTable({
ajax: {
url: '@Url.Action("Prods", "NewTest")',
method: 'post',
dataType: 'json',
dataSrc : ''
},
columns: [
{
title: 'Id',
data: 'id',
searchable: false,
sortable: false,
visible: false
},
{
title: 'Name',
data: 'name',
render: function (data, type, row) {
return '<a id="' + row.id + '" href="javascript:void(0)" onclick="detailsClick(this)">' + data + '</a>'
}
},
....Other Columns....
],
serverSide: true,
processing: true,
language: {
processing : "Please Wait ..."
}
})
})
</script>
这是我的MVC控制器
public IActionResult Prods(){
int draw = Convert.ToInt32(Request.Query["draw"]);
int StartIndex = Convert.ToInt32(Request.Query["start"]);
int PageSize = Convert.ToInt32(Request.Query["length"]);
int SortCol = Convert.ToInt32(Request.Query["order[0][column]"]);
string SortDir = Request.Query["order[0][dir]"];
string SearchField = Request.Query["search[value]"].FirstOrDefault()?.Trim();
..... Further Implementation .....
return new JsonResult(new
{
data = filteredData,
draw = Request.Query["draw"],
recordsFiltered = filteredData.Count(),
recordsTotal = 13
}
);
}
我在Controller中得到了空值,请帮帮我.
I got null values in Controller, Help me with this.
推荐答案
我无法获取用于排序的参数,即从datatble搜索到我的MVC Controller.
i cannot get parameters for sorting, Searching From datatble to my MVC Controller.
如果使用F12开发人员工具Network捕获对NewTest/Prods
动作方法的请求,则会发现服务器端处理相关数据是通过表单数据传递的,而不是通过url的查询字符串部分传递的.
If you use F12 developer tool Network to capture the request to NewTest/Prods
action method, you would find the server-side processing related data are passed through form data, not in query string part of url.
要在控制器操作中获取这些数据,您可以尝试:
To get these data in your controller action, you can try:
int draw = Convert.ToInt32(Request.Form["draw"]);
int StartIndex = Convert.ToInt32(Request.Form["start"]);
int PageSize = Convert.ToInt32(Request.Form["length"]);
int SortCol = Convert.ToInt32(Request.Form["order[0][column]"]);
string SortDir = Request.Form["order[0][dir]"];
string SearchField = Request.Form["search[value]"].FirstOrDefault()?.Trim();
测试结果
这篇关于如何在ASP.net Core中实现dataTables服务器端分页/搜索/排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!