![](/img/trans.png)
[英]how to pass extra parameter in Ajax AutoComplete for jQuery?
[英]How to pass extra parameter on AJAX call of jQuery DataTable
我使用DataTable文档中指示的以下代码传递参数。
视图:
$('#example').dataTable( {
"ajax": {
"url": "/Student/GetStudents",
"data": function ( d ) {
d.test= "some data";
}
}
});
控制器:
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);
}
尽管将“ test”参数传递给控制器,但“ param”参数中的值为null或0,并且导致数据表的返回空数据。 另一方面,如果我在数据表参数中使用以下行而不是AJAX调用,则所有param值都将正确传递给Controller(但使用AJAX调用,此行也会导致错误)。 我需要将额外的参数传递给Controller,并且必须使用AJAX调用。 传递参数值时如何传递呢?
"ajaxSource": "/Student/GetStudents",
您的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);
}
最后,我通过使用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)
});
},
...
无论如何,非常感谢您提供的有用答案。 投票+有用的...
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);
}
您可以创建一个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.