如何将c#DataTable传递给jquery DataTable

时间:2019-09-30 05:00:01

标签: jquery asp.net-mvc

我正在尝试将C#DataTable数据发送到Jquery DataTable,但是我不知道这怎么可能。 这是我的代码,但是不起作用。

  public JsonResult GetJsonBiltyList()
    {
        SqlDataAdapter sda = new SqlDataAdapter("SELECT o.*, (SELECT COUNT(*) FROM OrderVehicle_Clone WHERE OrderID = o.ID) AS Vehicles, (SELECT COUNT(*) FROM OrderConsignment_Clone WHERE OrderID = o.ID) AS Containers, (SELECT COUNT(*) FROM OrderProduct_Clone WHERE OrderID = o.ID) AS Products, (SELECT COUNT(*) FROM OrderDamage_Clone WHERE OrderID = o.ID) AS Damage, (SELECT COUNT(*) FROM OrderReimursable_Clone WHERE OrderID = o.ID) AS Reimursables, (SELECT COUNT(*) FROM OrderRecieveBy_Clone WHERE OrderID = o.ID) AS OrderRecieveBy_Clone FROM Order_Clone o", con);
        DataTable dt = new DataTable();
        sda.Fill(dt);
       // List<DataRow> SearchBiltyList = dt.AsEnumerable().ToList();
        //SearchBiltyDTO bilty = new SearchBiltyDTO();
        //bilty.PickUpLocation = dt.Rows[0]["PickUpLocation"].ToString();
        //bilty.DropLocation = dt.Rows[0]["DropLocation"].ToString();
        //bilty.Vehicles = Convert.ToInt64(dt.Rows[0]["Vehicles"]);
        //bilty.Containers = Convert.ToInt64(dt.Rows[0]["Containers"]);
        //bilty.Products = Convert.ToInt64(dt.Rows[0]["Products"]);
        //bilty.Damage = Convert.ToInt64(dt.Rows[0]["Damage"]);
        //bilty.Reimursables = Convert.ToInt64(dt.Rows[0]["Reimursables"]);

        return Json(new { data = dt.AsEnumerable().ToList() }, JsonRequestBehavior.AllowGet);
    }

1 个答案:

答案 0 :(得分:0)

Script.js

jquery数据表绑定的示例代码。

var oTable = $("#tblProduct").DataTable({
    "serverSide": true,
    "sAjaxSource": "/Product/GetProductList",
    "filter": false,
    "orderMulti": false,
    "responsive": true,
    "iDisplayLength": 10,
    "iDisplayStart": 0,
    "language": {
        "url": LanguageUrl,
    },
    //send server parameter
    "fnServerParams": function (aoData) {
        aoData.push({ "name": "ProductCode", "value": $("#ProductCode").val() });
        aoData.push({ "name": "ProductDescription", "value": $("#ProductDescription").val() });

    },
    "aaSorting": [[0, "asc"]],
    "columns": [
        { "data": "ProductDimension", "orderable": false, },
        { "data": "ProductCategory", "orderable": true, },
        { "data": "CurrentStock", "orderable": true, },
        { "data": "InOrder", "orderable": true, },
        { "data": "Planned", "orderable": true, },
        { "data": "Required", "orderable": false },
        { "data": "WeightPerPiece", "orderable": true },
        {
            "data": "Archived", "orderable": false,
            "render": function (data, type, row) {
                return row.Archived == false ? "Active" : "Inactive";
            }
        }

    ],
});

这是.cshtml代码

 <table id="tblProduct" class="table table-bordered table-striped" width="100%">
                        <thead>
                            <tr>
                                <th width="20%">@_Localizer["Product Code"]</th>
                                <th width="24%">@_Localizer["Description"]</th>
                                <th width="11%">@_Localizer["Product Dimension"]</th>
                                <th width="11%">@_Localizer["Product Category"]</th>
                                <th width="5%">@_Localizer["Stock"]</th>
                                <th width="7%">@_Localizer["In Order"]</th>
                                <th width="5%">@_Localizer["Planned"]</th>

                            </tr>
                        </thead>
                    </table>

我希望它对您有用