Datatable params null when passing custom parameter/argument to C# server-side processor

Datatable params null when passing custom parameter/argument to C# server-side processor

dpwilliams2dpwilliams2 Posts: 3Questions: 2Answers: 0
edited January 2017 in Free community support

I'm new to DataTables and am trying to get it coded into a C# MVC applications with server-side processing to increase performance.

My issue occurs when trying to pass a connection string to my server side processing code block. I was able to get the connection string to pass through, however it's causing all my DataTable params to be null.

$(document).ready(function () {
        var table = $('#myTable').DataTable({
            "dom": '<"pull-left"l><"pull-right"f>rt<"pull-left"i><"pull-right"p>',
            "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
            "processing": true,
            "serverSide": true,
            "stateSave": true,
            "responsive": true,
            "buttons": [ 'colvis' ],
            "ajax": {
                "url": "/AJAX/AuditMessage2",
                "data": { Connection: "@ViewBag.EnvironmentDataConnection" },
                "type": "POST",
                "datatype": "jsonp"
            },
            "columns": [
                    ...
            ],
            initComplete: function () {
                $('#myTable_filter input').unbind();
                $('#myTable_filter input').bind('keyup', function (e) {
                    if (e.keyCode == 13) {
                        table.search(this.value).draw();
                    }
                });
            }
        });
    });
[HttpPost]
        public ActionResult AuditMessage(DTParameters param, string Connection)
        {
            try
            {
                using (AuditMessageEntities db = new AuditMessageEntities(Connection))
                {
                    var results = (from a in db.Audits select a);

                    if (!(string.IsNullOrEmpty(param.Search.Value)))
                    {
                        results = results.Where(s =>
                            s.OrderID.Contains(param.Search.Value) ||
                            s.ProgramName.Contains(param.Search.Value) ||
                            s.PhysicalName.Contains(param.Search.Value) ||
                            s.MessageType.Contains(param.Search.Value) ||
                            s.AuditableEvent.Contains(param.Search.Value) ||
                            s.Platform.Contains(param.Search.Value) ||
                            s.DatabaseServer.Contains(param.Search.Value) ||
                            s.DatabaseName.Contains(param.Search.Value) ||
                            s.SiteCode.Contains(param.Search.Value));
                    }

                    string sortOrder = String.Empty;

                    if (!string.IsNullOrEmpty(param.SortOrder))
                    {
                        sortOrder = param.SortOrder;
                    }
                    else
                    {
                        sortOrder = "LocalDateTimeStamp Desc";
                    }


                    return Json(new
                    {
                        draw = param.Draw,
                        recordsFiltered = results.Count(),
                        recordsTotal = results.Count(),
                        data = results.OrderBy(sortOrder).Skip(param.Start).Take(param.Length).ToList()
                    }, JsonRequestBehavior.DenyGet);
                }
            }
            catch (Exception ex)
            {
                return Json(new { error = ex.Message });
            }
        }

/// <summary>
    /// The parameters sent by jQuery DataTables in AJAX queries.
    /// </summary>
    public class DTParameters
    {
        /// <summary>
        /// Draw counter.
        /// This is used by DataTables to ensure that the Ajax returns from server-side processing requests are drawn in sequence by DataTables (Ajax requests are asynchronous and thus can return out of sequence).
        /// This is used as part of the draw return parameter (see below).
        /// </summary>
        public int Draw { get; set; }

        /// <summary>
        /// An array defining all columns in the table.
        /// </summary>
        public DTColumn[] Columns { get; set; }

        /// <summary>
        /// An array defining how many columns are being ordering upon - i.e. if the array length is 1, then a single column sort is being performed, otherwise a multi-column sort is being performed.
        /// </summary>
        public DTOrder[] Order { get; set; }

        /// <summary>
        /// Paging first record indicator.
        /// This is the start point in the current data set (0 index based - i.e. 0 is the first record).
        /// </summary>
        public int Start { get; set; }

        /// <summary>
        /// Number of records that the table can display in the current draw.
        /// It is expected that the number of records returned will be equal to this number, unless the server has fewer records to return.
        /// Note that this can be -1 to indicate that all records should be returned (although that negates any benefits of server-side processing!)
        /// </summary>
        public int Length { get; set; }

        /// <summary>
        /// Global search value. To be applied to all columns which have searchable as true.
        /// </summary>
        public DTSearch Search { get; set; }

        /// <summary>
        /// Custom column that is used to further sort on the first Order column.
        /// </summary>
        public string SortOrder
        {
            get
            {
                return Columns != null && Order != null && Order.Length > 0
                    ? (Columns[Order[0].Column].Data + (Order[0].Dir == DTOrderDir.DESC ? " " + Order[0].Dir : string.Empty))
                    : null;
            }
        }

    }

    /// <summary>
    /// A jQuery DataTables column.
    /// </summary>
    public class DTColumn
    {
        /// <summary>
        /// Column's data source, as defined by columns.data.
        /// </summary>
        public string Data { get; set; }

        /// <summary>
        /// Column's name, as defined by columns.name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Flag to indicate if this column is searchable (true) or not (false). This is controlled by columns.searchable.
        /// </summary>
        public bool Searchable { get; set; }

        /// <summary>
        /// Flag to indicate if this column is orderable (true) or not (false). This is controlled by columns.orderable.
        /// </summary>
        public bool Orderable { get; set; }

        /// <summary>
        /// Specific search value.
        /// </summary>
        public DTSearch Search { get; set; }
    }

    /// <summary>
    /// An order, as sent by jQuery DataTables when doing AJAX queries.
    /// </summary>
    public class DTOrder
    {
        /// <summary>
        /// Column to which ordering should be applied.
        /// This is an index reference to the columns array of information that is also submitted to the server.
        /// </summary>
        public int Column { get; set; }

        /// <summary>
        /// Ordering direction for this column.
        /// It will be dt-string asc or dt-string desc to indicate ascending ordering or descending ordering, respectively.
        /// </summary>
        public DTOrderDir Dir { get; set; }
    }

    /// <summary>
    /// Sort orders of jQuery DataTables.
    /// </summary>
    public enum DTOrderDir
    {
        ASC,
        DESC
    }

    /// <summary>
    /// A search, as sent by jQuery DataTables when doing AJAX queries.
    /// </summary>
    public class DTSearch
    {
        /// <summary>
        /// Global search value. To be applied to all columns which have searchable as true.
        /// </summary>
        public string Value { get; set; }

        /// <summary>
        /// true if the global filter should be treated as a regular expression for advanced searching, false otherwise.
        /// Note that normally server-side processing scripts will not perform regular expression searching for performance reasons on large data sets, but it is technically possible and at the discretion of your script.
        /// </summary>
        public bool Regex { get; set; }
    }
}
This discussion has been closed.