C# 使用jquery DataTable的fnServerParams和aoData将数据发送到服务器在MVC4中不起作用
我想向服务器端(ASP.Net MVC4)发送jquery数据表的额外数据。有很多关于如何使用这个客户端的例子,但是我不能让它在服务器端工作 代码如下: javascript:C# 使用jquery DataTable的fnServerParams和aoData将数据发送到服务器在MVC4中不起作用,c#,jquery,asp.net-mvc-4,jquery-datatables,C#,Jquery,Asp.net Mvc 4,Jquery Datatables,我想向服务器端(ASP.Net MVC4)发送jquery数据表的额外数据。有很多关于如何使用这个客户端的例子,但是我不能让它在服务器端工作 代码如下: javascript: $(document).ready(function () { var oTable = $('#myDataTable').dataTable({ "bServerSide": true, "sAjaxSource": "SearchPatient/DataHandler
$(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; }
}
//
///类,该类封装DataTables插件发送的最常见参数
///
公共类JQueryDataTableParamModel
{
///
///fnServerparams,这应该是一个对象数组?
///
公共对象[]aoData{get;set;}
///
///DataTable发送的请求序列号,响应中必须返回相同的值
///
公共字符串sEcho{get;set;}
///
///用于过滤的文本
///
公共字符串搜索{get;set;}
///
///表中应显示的记录数
///
公共int iDisplayLength{get;set;}
///
///应显示的第一条记录(用于分页)
///
公共int iDisplayStart{get;set;}
///
///表中的列数
///
public int iColumns{get;set;}
///
///用于排序的列数
///
public int iSortingCols{get;set;}
///
///以逗号分隔的列名列表
///
公共字符串sColumns{get;set;}
///
///用于过滤的文本
///
公共字符串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<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);
}
public JsonResult ReportJson(SpecificNewParamModel Model)
{
//code omitted code for clarity
return Json(Return);
}
publicActionResult数据处理程序(JQueryDataTableParamModel参数)
{
if(param.aoData!=null)
{
//获取aoData的第一个元素。不工作,始终为空
字符串lSearchValue=param.aoData[0].ToString();
//进程搜索值
// ....
}
返回Json(新的
{
sEcho=param.sEcho,
iTotalRecords=97,
iTotalDisplayRecords=3,
aaData=新列表(){
新字符串[]{“1”、“IE”、“Redmond”、“USA”、“NL”},
新字符串[]{“2”、“Google”、“Mountain View”、“USA”、“NL”},
新字符串[]{“3”、“Gowi”、“Pancevo”、“塞尔维亚”、“NL”}
}
},
JsonRequestBehavior.AllowGet);
}
注意:操作处理程序被命中,因此获取数据的ajax调用也在工作,并且我的datatable被3行填充
问题是:aoData总是空的。我希望第一个元素包含“我的值”
非常感谢您的帮助 经过几个小时的搜索,终于找到了答案,把它贴在了这里。只需几分钟就能找到答案: 这就是诀窍: 在DataHandler中添加此行serverside:
var wantedValue = Request["more_data"];
因此,值在请求中,而不是在模型中
谢谢。值确实在模型中,但它们作为单个字段传递,而不是作为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<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
);
}
publicActionResult数据处理程序(JQueryDataTableParamModel参数){
///按名称引用字段,而不是作为aoData的成员
字符串lSearchValue=param.more_数据;
返回Json(
新{
sEcho=param.sEcho,
iTotalRecords=97,
iTotalDisplayRecords=3,
aaData=新列表(){
新字符串[]{“1”、“IE”、“Redmond”、“USA”、“NL”},
新字符串[]{“2”、“Google”、“Mountain View”、“USA”、“NL”},
新字符串[]{“3”、“Gowi”、“Pancevo”、“塞尔维亚”、“NL”}
}
},
JsonRequestBehavior.AllowGet
);
}
我将发布另一个答案,以展示避免代码重复的方法
您还可以将JQueryDataTableParamModel作为其他类的基类。Datatables将在同一个对象中发送自定义数据,因此您不能让模型直接绑定它,除非您的C#视图模型与Datatables发送的对象完全匹配
这可以通过@SetFreeByTruth实现,但如果您希望在整个项目中使用它,则可以进行一些代码复制。此表有更多\u数据
,如果您有另一个表,其中只有一个名为自定义\u数据
的属性,该怎么办?您需要使用多个字段填充对象,或者创建多个datatables视图模型,每个模型都包含自定义数据
对于您的场景,您可以使用继承。创建一个新类,如下所示:
//Inheritance from the model will avoid duplicate code
public class SpecificNewParamModel: JQueryDataTableParamModel
{
public string more_data { 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<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);
}
public JsonResult ReportJson(SpecificNewParamModel Model)
{
//code omitted code for clarity
return Json(Return);
}
如果发送DataTables请求并检查模型
,您可以看到它填充了自定义数据(更多数据
)和常用的DataTables数据