如何在使用 jquery 数据表时调用存储过程
How to call stored procedure while using jquery Data tables
我正在为我的项目使用 Jquery 数据 table,我正在研究 Asp.Net MVC 5 和 entity framework 6。我真正想做的是只需调用我的存储过程即可在我的 jquery 数据 table 中调用。现在我从我的数据库中调用 table 并且调用是 ajax 调用 jquery 数据 table.
这是我的 ajax 数据调用示例 table。
$('#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() 的代码在我的控制器中,如下所示,它从数据库调用 table,这是我需要调用存储过程的地方。
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);
}
}
为数据表
创建一个助手class
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; }
}
}
将 class 设置为控制器操作中的参数
[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: [
...
]
});
我通过这种方法得到它。希望对大家有所帮助。
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);
}
}
我正在为我的项目使用 Jquery 数据 table,我正在研究 Asp.Net MVC 5 和 entity framework 6。我真正想做的是只需调用我的存储过程即可在我的 jquery 数据 table 中调用。现在我从我的数据库中调用 table 并且调用是 ajax 调用 jquery 数据 table.
这是我的 ajax 数据调用示例 table。
$('#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() 的代码在我的控制器中,如下所示,它从数据库调用 table,这是我需要调用存储过程的地方。
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);
}
}
为数据表
创建一个助手classnamespace 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; }
}
}
将 class 设置为控制器操作中的参数
[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: [
...
]
});
我通过这种方法得到它。希望对大家有所帮助。
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);
}
}