简体   繁体   English

如何在jQuery DataTable的AJAX调用上传递额外的参数

[英]How to pass extra parameter on AJAX call of jQuery DataTable

I pass the parameters using the following code as indicated on DataTable Documentation . 我使用DataTable文档中指示的以下代码传递参数。


View: 视图:

$('#example').dataTable( {
      "ajax": {
           "url": "/Student/GetStudents",
           "data": function ( d ) {
               d.test= "some data";
           }
      }
});

Controller: 控制器:

public ActionResult GetStudents(JQueryDataTableParamModel param, string test)
{
    //code omitted for brevity

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

Although the "test" parameter is passed to the Controller, the values in the "param" parameter are null or 0 and that causing the datatable returns null data. 尽管将“ test”参数传递给控制器​​,但“ param”参数中的值为null或0,并且导致数据表的返回空数据。 On the other hand, if I use the following line instead of AJAX call in datatable parameters, all the values of param are passed to the Controller properly (but using AJAX call and this line also causes error). 另一方面,如果我在数据表参数中使用以下行而不是AJAX调用,则所有param值都将正确传递给Controller(但使用AJAX调用,此行也会导致错误)。 I need to pass extra parameter to the Controller and have to use AJAX call. 我需要将额外的参数传递给Controller,并且必须使用AJAX调用。 How can I pass it while passing param values? 传递参数值时如何传递呢?

"ajaxSource": "/Student/GetStudents",

Your javascript code: 您的JavaScript代码:

$('#example').dataTable( {
      "ajax": {
           "url": "/Student/GetStudents",
            type: 'GET',
            data: {
                    test1: "This test1 data ",
                    test2: "This test2 data"
                }
        }
});


public ActionResult GetStudents(JQueryDataTableParamModel param, string test)
{
    //code omitted for brevity

     //printing in params in controller with asp.net code. 
     print_r("Data from" ,param.test1 ,param.test2);

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

Finally I solved the problem by using fnServerData method as shown below. 最后,我通过使用fnServerData方法解决了该问题,如下所示。

"ajaxSource": "/Student/GetStudents",

//fnServerData used to inject the parameters into the AJAX call sent to the server-side
"fnServerData": function (sSource, aoData, fnCallback) {
    aoData.push({ "name": "test", "value": "some data" });
    $.getJSON(sSource, aoData, function (json) {
        fnCallback(json)
    });
},

...

Anyway, thanks a lot for the useful answers. 无论如何,非常感谢您提供的有用答案。 Voted+ the useful ones... 投票+有用的...

var finalArray = [];
var data = {'test':"some data","test1":"some data1"};
finalArray.push(data);
var rec = JSON.stringify(finalArray);

$('#example').dataTable( {
"ajax": {
   "url": "/Student/GetStudents",
   "data": rec
}
});

public ActionResult GetStudents(JQueryDataTableParamModel param,string test)
{
//code omitted for brevity

 //printing in params in controller with asp.net code. 
 print_r(json_decode(param));

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

You can create a json data string where you can pass extra parameters. 您可以创建一个json数据字符串,您可以在其中传递其他参数。

var data = {'test':"some data","test1":"some data1"};
    $('#example').dataTable( {
    "ajax": {
       "url": "/Student/GetStudents",
       "data": data
    }
});

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

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