我想将额外的数据发送到服务器端(ASP.Net MVC4)以获取我的 jquery 数据表。有很多关于如何在客户端工作的示例,但我无法让它在服务器端工作。
代码如下:
JavaScript:
$(document).ready(function () {
var oTable = $('#myDataTable').dataTable({
"bServerSide": true,
"sAjaxSource": "SearchPatient/DataHandler",
"fnServerParams": function (aoData) {
alert('in fnServerParams');
aoData.push( { "name": "more_data", "value": "my_value" } );
}
});
});
注意:警报响起,因此该功能本身正在运行。
我的模型类:
/// <summary>
/// Class that encapsulates most common parameters sent by DataTables plugin
/// </summary>
public class JQueryDataTableParamModel
{
/// <summary>
/// fnServerparams, this should be an array of objects?
/// </summary>
public object[] aoData { get; set; }
/// <summary>
/// Request sequence number sent by DataTable, same value must be returned in response
/// </summary>
public string sEcho { get; set; }
/// <summary>
/// Text used for filtering
/// </summary>
public string sSearch { get; set; }
/// <summary>
/// Number of records that should be shown in table
/// </summary>
public int iDisplayLength { get; set; }
/// <summary>
/// First record that should be shown(used for paging)
/// </summary>
public int iDisplayStart { get; set; }
/// <summary>
/// Number of columns in table
/// </summary>
public int iColumns { get; set; }
/// <summary>
/// Number of columns that are used in sorting
/// </summary>
public int iSortingCols { get; set; }
/// <summary>
/// Comma separated list of column names
/// </summary>
public string sColumns { get; set; }
/// <summary>
/// Text used for filtering
/// </summary>
public string oSearch { get; set; }
}
最后是我的 Controller :
public ActionResult DataHandler(JQueryDataTableParamModel param)
{
if (param.aoData != null)
{
// Get first element of aoData. NOT working, always null
string lSearchValue = param.aoData[0].ToString();
// Process search value
// ....
}
return Json(new
{
sEcho = param.sEcho,
iTotalRecords = 97,
iTotalDisplayRecords = 3,
aaData = new List<string[]>() {
new string[] {"1", "IE", "Redmond", "USA", "NL"},
new string[] {"2", "Google", "Mountain View", "USA", "NL"},
new string[] {"3", "Gowi", "Pancevo", "Serbia", "NL"}
}
},
JsonRequestBehavior.AllowGet);
}
注意:操作处理程序被命中,因此获取数据的 ajax 调用也正常工作,并且我的数据表填充了 3 行..
问题是:aoData 始终为空。我希望第一个元素包含“my_value”。
非常感谢任何帮助!
最佳答案
经过几个小时的寻找答案终于发布在这里。只需几分钟即可得出答案:
这可以解决问题:
在服务器端的 DataHandler 中添加此行:
var wantedValue = Request["more_data"];
因此该值位于请求中而不是模型中。
谢谢。
关于c# - 使用 jquery DataTable 的 fnServerParams 和 aoData 向服务器发送数据在 MVC4 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20349593/