如何在使用jquery数据表时调用存储过程

时间:2019-06-04 16:05:15

标签: jquery asp.net-mvc-5 datatables entity-framework-6

我正在为我的项目使用Jquery数据表,并且正在使用Asp.Net MVC 5和实体框架6。我真正想做的就是在我的jquery数据表中调用存储过程。现在,我正在从数据库中调用该表,该调用是对jquery数据表的ajax调用。

这是我对数据表的ajax调用的示例。

$('#studentTable').DataTable({
    "ajax": {
        "url": "/StructuredImportTgts/GetData",
        "type": "GET",
        "datatype": "json"
    },
    responsive: 'true',
    dom: 'Bfrtip',
    buttons: [
        'copy', 'excel', 'pdf'
    ],
    "columns": [
        { "data": "PART_NO" },
        { "data": "LEVEL" },
        { "data": "PART_NO" },
        { "data": "PART_NAME" },
        { "data": "L1QTY" },
        { "data": "PL1" },
        { "data": "PL2" },
        { "data": "PL3" },
        { "data": "SupplierLocID" },
        { "data": "SupplierLocID" },
        { "data": "Discrepancies" },
        { "data": "Comments" }
    ]

GETDATA()的代码在我的控制器中,如下所示,它从数据库中调用表,这是我需要调用存储过程的地方。

public ActionResult GetData()
{
    using (Dev_Purchasing_New_ModelEntities db = new Dev_Purchasing_New_ModelEntities())
    {
        db.Configuration.LazyLoadingEnabled = false;
        List<bomStructuredImportTgt> bomStructuredImportTgtList = db.bomStructuredImportTgts.ToList<bomStructuredImportTgt>();
        return Json(new { data = bomStructuredImportTgtList }, JsonRequestBehavior.AllowGet);
    }
}

2 个答案:

答案 0 :(得分:0)

为数据表创建助手类

namespace DataTableHelper
{
    public class DataTableModel
    {
        public int Draw { get; set; }
        public int Start { get; set; }
        public int Length { get; set; }
        public IEnumerable<Column> Columns { get; set; }
        public IEnumerable<Order> Order { get; set; }
        public Search Search { get; set; }
    }
    public class Column
    {
        public string Data { get; set; }
        public string Name { get; set; }
        public bool Searchable { get; set; }
        public bool Orderable { get; set; }
        public Search Search { get; set; }
    }

    public class Search
    {
        public string Value { get; set; }
        public string Regex { get; set; }
    }

    public class Order
    {
        public int Column { get; set; }
        public string Dir { get; set; }
    }
}

将该类设置为控制器操作中的参数

[HttpPost]
public JsonResult GetData(DataTableModel model)
{
    list can be anything
    var list = new List(); // list of records to be displayed in datatable
    return Json(new
            {
                draw = model.Draw,
                data = list,
                recordsTotal = list .Count,
                recordsFiltered = 0
            }, JsonRequestBehavior.AllowGet);

}

数据表设置

$('#datatable').DataTable({
    ajax: {
      url: '/MyController/GetData',
      type: "POST" // ajax type must be match to controllers action type
    },
    serverSide: false,
    processing: true,
    columns: [
    ...
    ]
});

答案 1 :(得分:0)

我通过这种方法得到它。我希望这对某人有帮助。

  public ActionResult GetData1()
    {
        using (Dev_Purchasing_New_ModelEntities db = new Dev_Purchasing_New_ModelEntities())
        {
            db.Configuration.LazyLoadingEnabled = false;
            var bomStructuredImportTgtList = db.usp_GetStructureTGT();
            return Json(new { data = bomStructuredImportTgtList }, JsonRequestBehavior.AllowGet);
        }
    }