我得到" ObjectContext实例已被处理,不能再用于需要连接的操作"当我尝试为关系SQL数据库实现jQuery DataTable时,我的Intranet应用程序上出现错误消息。
这是我的HomeController代码:
using CyberAssets.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CyberAssets.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult loaddata()
{
using (CyberAssetsEntities dc = new CyberAssetsEntities())
{
var data = dc.CyberAssets.OrderBy(a => a.Id).ToList();
return Json(new { data = data }, JsonRequestBehavior.AllowGet);
}
}
}
}
这是我使用DataTable查看的代码:
@section Scripts {
<script src="//cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function () {
$('#DataTbl').DataTable({
"ajax": {
"url": "/Home/loaddata",
"type": "GET",
"datatype": "json"
},
"columns": [
{ "data": "FacilityName", "autowidth": true },
{ "data": "FacilityType", "autowidth": true },
{ "data": "MachineType", "autowidth": true },
{ "data": "MachineFunctionDesc", "autowidth": true },
{ "data": "PhysicalLocation", "autowidth": true }
]
});
});
</script>
}
如何在HomeController的loaddata()
using
块中预加载我需要引用的对象,基本上&#34;避免&#34;延迟加载?
答案 0 :(得分:1)
您需要在DataTable初始化代码中添加以下行:
$('#DataTbl').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "/Home/loaddata",
"type": "GET",
"datatype": "json"
},
"columns": [
{ "data": "FacilityName", "autowidth": true },
{ "data": "FacilityType", "autowidth": true },
{ "data": "MachineType", "autowidth": true },
{ "data": "MachineFunctionDesc", "autowidth": true },
{ "data": "PhysicalLocation", "autowidth": true }
]
});
因此,您对DataTable的代码应如下所示:
Conversion_Date User_Name Last_Date_Touch Touch_Count
7/15/2017 A 6/17/2017 1
7/16/2017 B 6/24/2017 2
7/19/2017 A 6/20/2017 1
7/19/2017 C 6/29/2017 1