![](/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.