简体   繁体   English

如何在ASP.Net MVC中捕获jquery datatable服务器端数据源参数

[英]how to catch jquery datatable server side data source parameters in ASP.Net MVC

I'm using jQuery Datatable and server side proccessing as data source in my ASP.Net MVC application. 我在我的ASP.Net MVC应用程序中使用jQuery Datatable服务器端处理作为数据源。 I need to apply search term in my query but I don't know how to catch it. 我需要在查询中应用搜索字词,但我不知道如何捕捉它。

here is short version of my controller : 这是我的控制器的简短版本:

public JsonResult Index(POFilter m) {
   return Json(new {
      data = data,
      ...
   })
}

POFilter.cs POFilter.cs

public class POFilter
{
    public int start { get; set; }
    public int length { get; set; }
    public int draw { get; set; }
    public List<DT_Order> order { get; set; }
    public string[] search { get; set; }

    public POFilter()
    {
        start = 0;
        length = 10;
        draw = 1;
    }
}

public class DT_Order
{
    public int column { get; set; }
    public string dir { get; set; }
}

but for some reason the search property is always null. 但由于某种原因, search property始终为null。 you can see the parameters which are passed by jquery datatable: 您可以看到jquery datatable传递的参数:

order[0][column]:0
order[0][dir]:desc
start:0
length:10
search[value]:my search term
search[regex]:false

how can I get my search term from passed parameters? 如何从传递的参数中获取my search term

I'm using an object to hold the search result. 我正在使用一个对象来保存搜索结果。 Instead of using: 而不是使用:

public string[] search { get; set; }

I use: 我用:

public DataTableSearch Search { get; set; }

Where DataTableSearch is defined as: 其中DataTableSearch定义为:

public class DataTableSearch
{
    public string Value { get; set; }
    public string Regex { get; set; }
}

Let me know if that doesn't fix your issue (if you're still having an issue). 让我知道这是否不能解决您的问题(如果您仍然有问题)。

Use datatables.mvc5 nuget package to get typed modelbinder: 使用datatables.mvc5 nuget包获取键入的modelbinder:

public JsonResult GetData([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel)
{
    // requestModel.Start..
    // requestModel.Length..
    // requestModel.Columns..
}

Take a look at this project, it is a MVC / Jquery datatable implementation made by a friend. 看一下这个项目,它是一个由朋友制作的MVC / Jquery数据表实现。 I use this in almost all of my projects 我几乎在所有项目中都使用了

https://github.com/amoerie/generic-datatables https://github.com/amoerie/generic-datatables

its a bit hard to implement, but once it is in place, its easy to work with 实施起来有点困难,但是一旦安装到位,就很容易使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM