mhesabi
mhesabi

Reputation: 1140

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. 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

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. you can see the parameters which are passed by 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?

Upvotes: 1

Views: 1559

Answers (3)

o..o
o..o

Reputation: 1921

Use datatables.mvc5 nuget package to get typed modelbinder:

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

Upvotes: 1

BrianLegg
BrianLegg

Reputation: 1700

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:

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).

Upvotes: 1

Gelootn
Gelootn

Reputation: 601

Take a look at this project, it is a MVC / Jquery datatable implementation made by a friend. I use this in almost all of my projects

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

its a bit hard to implement, but once it is in place, its easy to work with

Upvotes: 0

Related Questions