2016-10-04 202 views
0

我使用以下代碼傳遞參數,如DataTable Documentation所示。如何通過AJAX調用jQuery 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); 
} 

雖然 「測試」 參數傳遞到控制器,「參數」參數中的值爲空或0,並導致數據表返回空數據。另一方面,如果我在數據表參數中使用下面的行而不是AJAX調用,則param的所有值都會正確傳遞給控制器​​(但使用AJAX調用並且此行也會導致錯誤)。我需要傳遞額外的參數給控制器,並且必須使用AJAX調用。我怎樣才能傳遞參數值?

"ajaxSource": "/Student/GetStudents", 
+0

有關這個問題的任何想法? –

回答

0

您可以創建一個json數據字符串,您可以在其中傳遞額外的參數。

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

我已經可以將參數傳遞給控制器​​,但它會導致參數參數傳遞爲null或0.通過使用您的示例,會出現相同的結果,我只需要傳遞param值而不使用**「ajaxSource」:「/ Student/GetStudents「**線。任何想法? –

+0

使用JSON.stringify(數據)併發送數據。在控制器結束使用JSON.parse(params)解析參數。檢查它是否正在工作..? –

+0

請問您可以通過以下兩種方式更新您的答案:AJax調用和控制器(stringfy轉換)。 –

2

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); 
} 
+0

關於[this]的任何ide(http://stackoverflow.com/questions/40571553/jquery-datatable-individual-column-searching-on-table-header)問題? –

1
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); 
} 
1

最後,我通過使用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) 
    }); 
}, 

... 

無論如何,非常感謝有用的答案。投票+有用的...

相關問題