似乎无法使用 MVC 和实体框架返回 ASP.NET 模型



这是我的代码,建立在ASP。. NET MVC和实体框架:

[HttpPost]
[Route("DeskBooking")]
public JsonResult DeskBooking(string dpStart, string dpEnd, int tmStart, int tmEnd)
{
DateTime dpStartCon = DateTime.Parse(GetDateStart(dpStart));
DateTime dpEndCon = DateTime.Parse(GetDateEnd(dpEnd));

using (Models.nhsdmsEntities ctx = new Models.nhsdmsEntities())
{
List<Models.tblDeskBooking> tblDB = ctx.tblDeskBookings
.Where(x => dpStartCon <= x.DateStart && 
x.DateEnd <= dpEndCon && 
tmStart >= x.TimeStart && 
tmEnd <= x.TimeEnd).ToList();
return Json(new { data = tblDB }, JsonRequestBehavior.AllowGet);
}
}

tblDB有3行,但仍然在客户端,我得到这个错误:

当前web请求执行过程中产生未处理异常
[ObjectDisposedException: ObjectContext实例已被处置,不能再用于需要连接的操作]

客户端代码:

$(document).on("click", "#btnBookDeskEmp", function () {
var dpStart = $("#dpStart").val();
var dpEnd = $("#dpEnd").val();
var tmStart = $("#tmStart").val();
var tmEnd = $("#tmEnd").val();
AjaxReq(function (data) {
}, "DeskBooking", { dpStart: dpStart, dpEnd: dpEnd, tmStart: parseInt(tmStart), tmEnd: parseInt(tmEnd) });
})
function AjaxReq(callback, action, data) {
$.ajax({
url: "/Home/" + action,
method: "POST",
data: data,
})
.done(function (data) {
callback(data);
})
.fail(function (e) {
alert("error");
})
.always(function () {
console.log("complete");
});
}
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace NHSDMS.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class nhsdmsEntities : DbContext
{
public nhsdmsEntities()
: base("name=nhsdmsEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<tblDesk> tblDesks { get; set; }
public virtual DbSet<tblRoom> tblRooms { get; set; }
public virtual DbSet<tblDeskBooking> tblDeskBookings { get; set; }
}
}

在edmx文件中,我不得不删除导航属性中的所有内容,因为这会弄乱我的命名空间。如果你想要更多的信息,我可以显示截图。

最新更新