模型绑定新的Datatables 1.10参数

在Datatables 1.10中,ajax服务器端参数已更改

public class DataTableParamModel { 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; } } 

to(API Here http://datatables.net/manual/server-side )

 columns[i][data] columns[i][name] columns[i][orderable] columns[i][search][regex] columns[i][search][value] columns[i][searchable] ... draw length order[i][column] order[i][dir] ... search[regex] search[value] start 

有些很容易绑定

 public class DataTableParamModel { public string draw { get; set; } public int length{ get; set; } public int start { get; set; } } 

但是新的数组格式看起来很棘手。

映射新参数格式的新合适模型是什么?

这是一个模型绑定器和类,它将绑定这些新参数……

参数型号:

 [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 - ie 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 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 }; } } 

用法:

MyController.cs

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

MVC6

如果您要使用MVC6,则不再需要模型绑定器,因为MVC6将JQueryFormValueProvider包含在可以绑定这些值的默认模型绑定器中。

但是,模型类本身可能仍然有用。

在2.1.0中有一个错误需要修复,它不允许绑定HttpGet但仍适用于HttpPost

试一试@shoe: datatables-mvc项目 : https : //github.com/ALMMa/datatables-mvc

我更改了我的javascript以使用遗留的ajax params选项,该选项使用旧参数发送到服务器。 这是通过$.fn.dataTable.ext.legacy.ajax = true; 所以现在我的代码变得像……

 $.fn.dataTable.ext.legacy.ajax = true; var datatable = $('#data-table').DataTable({ "processing": true, "serverSide": true, "ajax": "MyController/AjaxHandlerPaging", "pageLength": 25, "order": [[2, 'desc']], "columns": [] }); 

移动到1.10时我遇到了同样的问题。 基本上,我改变了我的参数类(摆脱不支持的参数):

 public class jQueryDataTableParamModel { ///  /// Request sequence number sent by DataTable, /// same value must be returned in response ///  public string draw { get; set; } ///  /// Number of records that should be shown in table ///  public int length { get; set; } ///  /// First record that should be shown(used for paging) ///  public int start { get; set; } } 

在我的控制器中,我得到搜索值,排序顺序和排序列,如下所示:

  var searchString = Request["search[value]"]; var sortColumnIndex = Convert.ToInt32(Request["order[0][column]"]); var sortDirection = Request["order[0][dir]"]; // asc or desc 

知道这篇文章是2岁但是对于想要在ASP.Net Core MVC 6中使用它的人。这是由@ Shoe提供的转换/升级答案

型号粘合剂:

 using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.AspNetCore.Mvc.ModelBinding; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace TrackingAndTraining.Models { ///  /// Model Binder for DTParameterModel (DataTables) ///  public class DTModelBinder : IModelBinder { public Task BindModelAsync(ModelBindingContext bindingContext) { var request = bindingContext.ActionContext.HttpContext.Request.Form; // 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 (!string.IsNullOrEmpty(request["order[" + o + "][column]"])) { 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 (!string.IsNullOrEmpty(request["columns[" + c + "][name]"])) { 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++; } var result = new DTParameterModel { Draw = draw, Start = start, Length = length, Search = search, Order = order, Columns = columns }; bindingContext.Result = ModelBindingResult.Success(result); return TaskCache.CompletedTask; } } } 

参数型号:

 using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace TrackingAndTraining.Models { [ModelBinder(BinderType = 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 - ie 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 List Order { get; set; } ///  /// Collection of all columns in the table ///  public List 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; } } } 

再次归功于@ Shoe的原始post。