json - 似乎无法使用 ASP.NET MVC 和实体框架返回我的模型
问题描述
这是我的代码,基于 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 文件中,我不得不从导航属性中删除所有内容,因为这弄乱了我的命名空间。如果您想了解更多信息,我可以显示截图。
推荐阅读
- amazon-web-services - SaaS 客户的自定义域名
- ckeditor4.x - ckeditor语法上的JS控制台错误
- java - ProjectManager.getDefault().findProject 多次尝试后无法正常工作
- node.js - 错误:将循环结构转换为 JSON --> 从具有构造函数“拓扑”的对象开始 | 属性 's' -> 带有 .... 在 Nodejs Express 中的对象
- javascript - 如何有效地查询大字段数组的对象?
- javascript - 我的用于检测明/暗模式的 JS 无法正常工作
- multithreading - grpc 客户端使用异步回调需要锁定并等待 std::condition_variable
- javascript - 提交表单时setTimeout函数在iphone Ios14中不起作用
- stripe-payments - MERN 堆栈开发 - StripeAuthenticationError
- linux - 如何安装 kernel-devel 和 kernel-headers mathching 5.x 内核?