Model binding new Datatables 1.10 parameters

前端 未结 6 1415
南方客
南方客 2021-02-01 16:44

In Datatables 1.10 the ajax server side parameters changed from

public class DataTableParamModel
{ 
    public string sEcho{ get; set; }
    public string sSearc         


        
6条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-01 17:31

    Here is a model binder and class that will bind these new parameters...

    Parameter Model:

    [ModelBinder(typeof(DTModelBinder))]
    public class DTParameterModel
    {
        /// 
        /// Draw counter. This is used by DataTables to ensure that the Ajax returns from 
        /// server-side processing requests are drawn in sequence by DataTables 
        /// 
        public int Draw { get; set; }
    
        /// 
        /// Paging first record indicator. This is the start point in the current data set 
        /// (0 index based - i.e. 0 is the first record)
        /// 
        public int Start { get; set; }
    
        /// 
        /// 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!)
        /// 
        public int Length { get; set; }
    
        /// 
        /// Global Search for the table
        /// 
        public DTSearch Search { get; set; }
    
        /// 
        /// Collection of all column indexes and their sort directions
        /// 
        public IEnumerable Order { get; set; }
    
        /// 
        /// Collection of all columns in the table
        /// 
        public IEnumerable Columns { get; set; }
    }
    
    /// 
    /// Represents search values entered into the table
    /// 
    public sealed class DTSearch
    {
        /// 
        /// Global search value. To be applied to all columns which have searchable as true
        /// 
        public string Value { get; set; }
    
        /// 
        /// 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
        /// 
        public bool Regex { get; set; }
    }
    
    /// 
    /// Represents a column and it's order direction
    /// 
    public sealed class DTOrder
    {
        /// 
        /// 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
        /// 
        public int Column { get; set; }
    
        /// 
        /// Ordering direction for this column. It will be asc or desc to indicate ascending
        /// ordering or descending ordering, respectively
        /// 
        public string Dir { get; set; }
    }
    
    /// 
    /// Represents an individual column in the table
    /// 
    public sealed class DTColumn
    {
        /// 
        /// Column's data source
        /// 
        public string Data { get; set; }
    
        /// 
        /// Column's name
        /// 
        public string Name { get; set; }
    
        /// 
        /// Flag to indicate if this column is orderable (true) or not (false)
        /// 
        public bool Orderable { get; set; }
    
        /// 
        /// Flag to indicate if this column is searchable (true) or not (false)
        /// 
        public bool Searchable { get; set; }
    
        /// 
        /// Search to apply to this specific column.
        /// 
        public DTSearch Search { get; set; }
    }
    

    Model Binder:

    /// 
    /// Model Binder for DTParameterModel (DataTables)
    /// 
    public class DTModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            base.BindModel(controllerContext, bindingContext);
            var request = controllerContext.HttpContext.Request;
            // Retrieve request data
            var draw = Convert.ToInt32(request["draw"]);
            var start = Convert.ToInt32(request["start"]);
            var length = Convert.ToInt32(request["length"]);
            // Search
            var search = new DTSearch
            {
                Value = request["search[value]"],
                Regex = Convert.ToBoolean(request["search[regex]"])
            };
            // Order
            var o = 0;
            var order = new List();
            while (request["order[" + o + "][column]"] != null)
            {
                order.Add(new DTOrder
                {
                    Column = Convert.ToInt32(request["order[" + o + "][column]"]),
                    Dir = request["order[" + o + "][dir]"]
                });
                o++;
            }
            // Columns
            var c = 0;
            var columns = new List();
            while (request["columns[" + c + "][name]"] != null)
            {
                columns.Add(new DTColumn
                {
                    Data = request["columns[" + c + "][data]"],
                    Name = request["columns[" + c + "][name]"],
                    Orderable = Convert.ToBoolean(request["columns[" + c + "][orderable]"]),
                    Searchable = Convert.ToBoolean(request["columns[" + c + "][searchable]"]),
                    Search = new DTSearch
                    {
                        Value = request["columns[" + c + "][search][value]"],
                        Regex = Convert.ToBoolean(request["columns[" + c + "][search][regex]"])
                    }
                });
                c++;
            }
    
            return new DTParameterModel
            {
                Draw = draw,
                Start = start,
                Length = length,
                Search = search,
                Order = order,
                Columns = columns
            };
        }
    }
    

    Usage:

    MyController.cs

    public JsonResult DataTablesList(DTParameterModel model)
    {
        ...
    }
    

    MVC6

    If you're going to MVC6 you no longer need the model binder as MVC6 includes a JQueryFormValueProvider into the default model binder that can bind these values.

    The model classes themselves may still be useful, however.

    There is a bug to be fixed in 2.1.0 that doesn't allow binding for HttpGet but still works for HttpPost

提交回复
热议问题