使用fnServerParams将数据发送到服务器,使用jquery DataTable的aoData在MVC4中不起作用
我想为我的jquery数据表向serverside(ASP.Net MVC4)发送额外的数据。 关于如何使用这个客户端有很多例子,但是我无法让它在服务器端工作。
这是代码:
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" } ); } }); });
注意:警报响起,因此函数本身正在工作。
我的模特课:
/// /// Class that encapsulates most common parameters sent by DataTables plugin /// public class JQueryDataTableParamModel { /// /// fnServerparams, this should be an array of objects? /// public object[] aoData { get; set; } /// /// Request sequence number sent by DataTable, same value must be returned in response /// public string sEcho { get; set; } /// /// Text used for filtering /// public string sSearch { get; set; } /// /// Number of records that should be shown in table /// public int iDisplayLength { get; set; } /// /// First record that should be shown(used for paging) /// public int iDisplayStart { get; set; } /// /// Number of columns in table /// public int iColumns { get; set; } /// /// Number of columns that are used in sorting /// public int iSortingCols { get; set; } /// /// Comma separated list of column names /// public string sColumns { get; set; } /// /// Text used for filtering /// public string oSearch { get; set; } }
最后我的控制器:
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() { 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始终为null。 我希望第一个元素保持“my_value”。
任何帮助深表感谢!
搜索了几个小时后找到答案,最后在这里发布。 只需在几分钟内得出答案:
这样做的诀窍:
在DataHandler中添加此行服务器端:
var wantedValue = Request["more_data"];
因此值在请求中而不在模型中。
谢谢。
值(s)确实在模型中,但它们作为单独的字段传递,而不是作为aoData
元素aoData
:
public class JQueryDataTableParamModel { /// The "more_data" field specified in aoData public string more_data { get; set; } public string sEcho { get; set; } public string sSearch { get; set; } public int iDisplayLength { get; set; } public int iDisplayStart { get; set; } public int iColumns { get; set; } public int iSortingCols { get; set; } public string sColumns { get; set; } public string oSearch { get; set; } }
用法:
public ActionResult DataHandler(JQueryDataTableParamModel param) { /// Reference the field by name, not as a member of aoData string lSearchValue = param.more_data; return Json( new { sEcho = param.sEcho, iTotalRecords = 97, iTotalDisplayRecords = 3, aaData = new List() { new string[] {"1", "IE", "Redmond", "USA", "NL"}, new string[] {"2", "Google", "Mountain View", "USA", "NL"}, new string[] {"3", "Gowi", "Pancevo", "Serbia", "NL"} } }, JsonRequestBehavior.AllowGet ); }
我将发布另一个答案,以显示避免代码重复的方法。
您还可以将JQueryDataTableParamModel作为其他人的基类。 数据表将在同一对象中发送自定义数据,因此您无法使模型直接绑定它,除非您的C#视图模型与DataTables发送的对象完全匹配。
这可以通过@SetFreeByTruth回答来实现,但如果您希望在整个项目中使用它,则可能会有一些代码重复。 这个表有more_data
,如果你有另一个只有一个名为custom_data
的属性的表custom_data
办? 您需要使用多个字段填充对象或创建多个数据表视图模型,每个模型都包含您的自定义数据。
对于您的方案,您可以使用inheritance。 像这样创建一个新类:
//Inheritance from the model will avoid duplicate code public class SpecificNewParamModel: JQueryDataTableParamModel { public string more_data { get; set; } }
在控制器中使用它:
public JsonResult ReportJson(SpecificNewParamModel Model) { //code omitted code for clarity return Json(Return); }
如果您发送DataTables请求并检查Model
,您可以看到它充满了您的自定义数据( more_data
)和通常的DataTables数据。