带有自定义对象参数的jquery数据表

时间:2016-12-26 14:11:40

标签: jquery asp.net-mvc datatables

我正在使用MVC和Jquery Data表。 我只想将自己的参数作为类与jquery datatable param一起传递。 这是我使用的代码,但它不起作用,

index.js

var options = {};
options.bProcessing = true;
options.bServerSide = true;
options.sAjaxSource = urlContent + 'Employee/GetAll';
options.bPaginate = true;
options.sPaginationType = "full_numbers";
options.iDisplayLength = 10;
options.bAutoWidth = true;
options.aoColumns = cols;
options.fnServerData = function (sSource, aoData, fnCallback) {
    $.ajax( {
        "dataType": 'json',
        "type": "GET",
        "url": sSource,
        "data": aoData,
        "success": function (data) {  },
        "failure": function (data) {  }
    } );
};

options.fnServerParams = function (aoData) {

    aoData.push({ "name": "employeeModel", "value": { Name : "testing", Age:25} });   
};

    var table = $('#' + tableId).DataTable(options);

mu控制器是,

 public JsonResult GetReportData(JQueryDataTableParamViewModel param, EmployeeViewModel employeeModel)
{
   return new List<Employee>();  //// Loads from Database
}

employeeModel始终为空。哪里我做错了。请帮我。

1 个答案:

答案 0 :(得分:0)

我没有使用构造函数来完成它,它将选项作为参数,但我曾经用其他方式来做,这里是代码:

$('#assets-data-table').DataTable({
                    "serverSide": true,
                    "processing": true,
                    "ajax": {
                        "url": "@Url.Action("Get","Asset")",
                        "data": function (data) {

                            data.FacilitySite = $("#FacilitySite").val();
                            data.Building = $("#Building").val();
                            data.Manufacturer = $("#Manufacturer").val();
                            data.Status = $("#Status").val();
                        }
                    },
                    "columns": [
                        { "title": "Bar Code", "data": "BarCode", "searchable": true },
                        { "title": "Manufacturer", "data": "Manufacturer", "searchable": true },
                        { "title": "Model", "data": "ModelNumber", "searchable": true },
                        { "title": "Building", "data": "Building", "searchable": true },
                        { "title": "Room No", "data": "RoomNo" },
                        { "title": "Quantity", "data": "Quantity" },
                        {
                            "title": "Actions",
                            "data": "AssetID",
                            "searchable": false,
                            "sortable": false,
                            "render": function (data, type, full, meta) {
                                return '<a href="@Url.Action("Edit","Asset")?id=' + data + '" class="editAsset">Edit</a> | <a href="@Url.Action("Details","Asset")?id=' + data + '">Details</a> | <a href="@Url.Action("Delete","Asset")?id=' + data + '">Delete</a>';
                            }
                        }
                    ],
                    "lengthMenu": [[10, 25, 50, 100], [10, 25, 50, 100]],
                    initComplete: function () {
                        var input = $('.dataTables_filter input').unbind(),
                            self = this.api(),
                            $searchButton = $('<button>')
                                       .text('search')
                                       .addClass("btn btn-default")
                                       .css("margin-left", "10px")
                                       .click(function () {
                                           self.search(input.val()).draw();
                                       })

                        $('.dataTables_filter').append($searchButton);
                    },
                    "createdRow": function (row, data, index) {
                        if (data["Building"] == null)
                            return;
                        switch (data["Building"].toLowerCase()) {
                            case 'west':
                                $('td:not(:last-child)', row).css({
                                    'background': 'red',
                                    'color': 'white'
                                });
                                break;
                        }
                    }
                });

我们在ajax调用中指定自定义参数:

 "data": function (data) {

                            data.FacilitySite = $("#FacilitySite").val();
                            data.Building = $("#Building").val();
                            data.Manufacturer = $("#Manufacturer").val();
                            data.Status = $("#Status").val();
                        }

动作如下:

 public ActionResult Get([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel, AdvancedSearchViewModel searchViewModel)
 {


 }