【问题标题】:jQuery datatable using ajax POST method to send custom objectjQuery 数据表使用 ajax POST 方法发送自定义对象
【发布时间】:2016-10-28 17:04:05
【问题描述】:

我想使用 jquery 数据表默认参数将自定义对象发送到我的控制器方法,用于分页、排序、搜索和记录数绑定。

问题是: 当我使用 datatable ajax 将对象发布到服务器时,它成功发布了我的对象并发送了长度参数,开始和绘制但停止发送用于排序 n 搜索的参数。 当我做 GET 请求时,它会发送所有参数,但不仅仅是我的自定义对象,显然它不应该

我想发送带有数据表默认参数的自定义对象以进行分页和排序 它的解决方案是什么?

代码如下:

 $('#example').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "/Color/Fetch",
            "type": 'Post',
            // "dataType": "json"
            data: { color: $scope.colorSearch }
            //  dataSrc: 'data'
        },
        "columns": [
            { "data": "Active" },
            { "data": "Company_Id" },
            { "data": "Id" },
            { "data": "IsActive" },
            { "data": "Name" }
        ]
    });

控制器动作:

 public JsonResult Fetch(Color color, int draw, int start, int length)
    {


        string search = Request.QueryString["search[value]"];
        int sortColumn = -1;
        string sortDirection = "asc";
        if (length == -1)
        {
            length = 90;
        }

        // note: we only sort one column at a time
        if (Request.QueryString["order[0][column]"] != null)
        {
            sortColumn = int.Parse(Request.QueryString["order[0][column]"]);
        }
        if (Request.QueryString["order[0][dir]"] != null)
        {
            sortDirection = Request.QueryString["order[0][dir]"];
        }}

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 datatables


    【解决方案1】:

    尝试使用以下方法:

    $('#example').DataTable({
        "processing": true,
        "serverSide": true,
        /*
        "ajax": {
            "url": "/Color/Fetch",
            "type": 'Post',
            // "dataType": "json"
            data: { color: $scope.colorSearch }
            //  dataSrc: 'data'
        },
        */      
    
        "ajaxSource": "/Color/Fetch",
        //fnServerData method used to inject the parameters into the AJAX call sent to the server-side
        "fnServerData": function (sSource, aoData, fnCallback) {
            aoData.push({ "name": "color", "value": "Blue" }); // Add some extra data to the sender
            $.getJSON(sSource, aoData, function (json) {
                /* Do whatever additional processing you want on the callback, then tell DataTables */
                fnCallback(json);
            });
        },
    
        "columns": [
            { "data": "Active" },
            { "data": "Company_Id" },
            { "data": "Id" },
            { "data": "IsActive" },
            { "data": "Name" }
        ]
    });
    

    希望这会有所帮助...

    【讨论】:

      猜你喜欢
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      相关资源
      最近更新 更多