How to pass value from controller to jquery datatable

强颜欢笑 提交于 2019-12-11 13:54:02

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/25260672/how-to-pass-value-from-controller-to-jquery-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!