【问题标题】:MVC4 Bind DataTable request parameters to action filterMVC4 将 DataTable 请求参数绑定到操作过滤器
【发布时间】:2016-03-11 15:13:40
【问题描述】:

我正在尝试为 jquery-datatable 实现服务器端分页和排序。 但问题是我无法将数据表发布的数据绑定到我的操作模型 进行排序和过滤

这里是jquery-datatable ajax请求发布的数据

draw:5
columns[0][data]:FirstName
columns[0][name]:FirstName
columns[0][searchable]:true
columns[0][orderable]:true
columns[0][search][value]:
columns[0][search][regex]:false
columns[1][data]:LastName
columns[1][name]:LastName
columns[1][searchable]:true
columns[1][orderable]:true
columns[1][search][value]:
columns[1][search][regex]:false
......
columns[n][data]:Position
columns[n][name]:Position
columns[n][searchable]:true
columns[n][orderable]:true
columns[n][search][value]:
columns[n][search][regex]:false
order[0][column]:1
order[0][dir]:desc
start:0
length:10
search[value]:
search[regex]:false

而我的操作方法是:

 public JsonResult GetGridData(GridFilter filter)
{ ....}

and my model classes are

    public class GridFilter
    {
        public int draw { get; set; }

        public List<ColModel> columns { get; set; }
        public List<Order> order { get; set; }

        public int start {get;set;}

        public int length {get;set;}

        public search search { get; set; }
    }

    public class ColModel
    {
        public string data { get; set; }

        public string name { get; set; }

        public string searchable { get; set; }

        public string orderable { get; set; }
    }

    public class Order
    {
        public string dir { get; set; }
        public string column { get; set; }
    }

    public class search
    {
        public string value {get;set;}
        public string regex {get;set;}
    }

如何使用默认的 mvc 模型绑定器正确绑定数据。

谢谢

【问题讨论】:

    标签: asp.net-mvc datatables


    【解决方案1】:

    确保您的模型属性具有与 here. 定义的相同数据类型

    此外,您的模型已经走得太远了。它们作为单独的参数发送,因此您不需要 GridFilter 模型,它们应该像这样接收:

        [HttpPost]
        public JsonResult GetGridData(List<ColModel> columns, List<Order> order, Search search, int? start, int? length, int? draw)
        {
    
        }
    

    【讨论】:

    • 尝试将[HttpPost] 添加到您的操作中
    • 抱歉,您的模型中的某些数据类型不正确
    • 我用一个链接更新了我的答案,该链接将告诉您每个属性使用哪些数据类型
    猜你喜欢
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    相关资源
    最近更新 更多