使用fnServerParams和aoData为jquery DataTable向服务器发送数据在MVC4中不起作用 [英] Sending data to the server with fnServerParams and aoData for jquery DataTable does not work in MVC4

查看:279
本文介绍了使用fnServerParams和aoData为jquery DataTable向服务器发送数据在MVC4中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向我的jquery数据表发送额外的数据到服务器端(ASP.Net MVC4).关于如何使用此客户端有很多示例,但是我无法在服务器端使用它.

I want to send extra data to serverside (ASP.Net MVC4) for my jquery datatable. There are many examples on how to this client side, but I can't get it to work on the serverside.

这是代码:

javascript:

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

    });
});

注意:警报会关闭,因此该功能本身就可以正常工作.

Note: the alert goes off, so the function itself is working.

我的模型班:

    /// <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; }

    }

最后是我的控制器:

   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行.

Note: the action handler gets hit, so the ajax call to get data is also working and my datatable gets filled with 3 rows..

问题是:aoData始终为null.我希望第一个元素包含"my_value".

The problem is: aoData is always null. I expect the first element to hold "my_value".

非常感谢您的帮助!

推荐答案

搜索了数小时后,终于找到了答案.仅在几分钟内得出答案:

After searching for hours to find the answer finally posted it here. Only to come up with the answer in minutes:

这可以解决问题:

将此行添加到DataHandler的服务器端:

Add this line serverside in the DataHandler:

var wantedValue = Request["more_data"];

因此,值在请求中,而不在模型中.

So the value is in the request and not in the model.

谢谢.

这篇关于使用fnServerParams和aoData为jquery DataTable向服务器发送数据在MVC4中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆