sajju217
sajju217

Reputation: 467

How to pass value from controller to jquery datatable

My datatable scripting code is:

$('#datatable').dataTable({

        "processing": true,
        "bServerSide": true,
        "sAjaxSource": "/Employee/AjaxHandler",
        "fnServerParams": function ( aoData ) {
            aoData.push( { "name": "start", "value": something } );
        }

and this is calling the method AjaxHandler in controller:

    public ActionResult AjaxHandler(JQueryDataTableParamModel param,int start)
                {
              ...
return Json(new
            {
                sEcho = param.sEcho,
                iTotalRecords = count,
                iTotalDisplayRecords = count,
                aaData = result
            },
            JsonRequestBehavior.AllowGet);
                }

I want to return a variable with a value from AjaxHandler method that will be assigned to the property "value" inside aoData.push() that will be assigned to the parameter named "start" in the AjaxHandler method when called again during paging/sorting/filtering.How can I do that

Upvotes: 1

Views: 7875

Answers (2)

markpsmith
markpsmith

Reputation: 4918

I'm assuming you're trying to get the datatable functionality (paging etc) working using MVC? The link that @Se0ng11 added is very good, you should persevere with it.

To give you a bit of a start, you will need to create a class for JQueryDataTableParamModel :

public class jQueryDataTableParamModel
{
    public string sEcho { get; set; }
    public string sSearch { get; set; }
    public int iDisplayLength { get; set; }
    public int iDisplayStart { get; set; }
    public int iColumns { get; set; }
    public int iSortingCols { get; set; }
    public string sColumns { get; set; }
}

This model is passed to the contoller populated with paging,sorting & filtering info - you don't need to add aoData.push.

In your controller action:

public ActionResult AjaxHandler(JQueryDataTableParamModel param)
{
    // query to get data
    var data = from a in b select a;

    var count = data.count();

    //use the paging params on your datasource
    var displayedItems = data.Skip(param.iDisplayStart).Take(param.iDisplayLength);
    //  project results into json for datatable
    var result = from r in filteredItems
        select new object[]
        {
          r.Id,
          r.Name
          ...
        }

     return Json(new
     {
          param.sEcho,
          iTotalRecords = count,
          iTotalDisplayRecords = count,
          aaData = result
      }, JsonRequestBehavior.AllowGet);
}

You can put a breakpoint in the controller method and have a look at what's being passed in JQueryDataTableParamModel

Upvotes: 0

Se0ng11
Se0ng11

Reputation: 2333

refer to the below link http://www.codeproject.com/Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part

this is quite a complete step by step guide on how you can achieve the goal to create ajax jquery datatable

Upvotes: 2

Related Questions