ObjectContext 实例已释放,不能再用于需要连接错误级联下拉列表的操作
本文关键字:错误 连接 级联 下拉列表 操作 用于 实例 释放 不能 ObjectContext | 更新日期: 2023-09-27 18:34:57
我尝试在MVC4中级联下拉列表。我有 2 个下拉列表 1 - 类别2 - 子类别。
当用户创建新产品时,他需要选择类别,然后选择与该类别相关的子类别。我和杰森一起使用阿贾克斯。
public ActionResult Create()
{
List<Category> allcategories = new List<Category>();
List<SubCategory> allSubCategories = new List<SubCategory>();
using (WebStoreEntities1 db1 = new WebStoreEntities1())
{
allcategories = db1.Categories.OrderBy(x => x.CategoryName).ToList();
}
ViewBag.categoryID = new SelectList(db.Categories, "CategoryId", "CategoryName");
ViewBag.SubCategoryID = new SelectList(allSubCategories, "SubCategoryId", "SubCategoryName");
return View(main);
}
在 html 页面中,Jquery 代码:
$(document).ready(function () {
var $SubCategoryID = $('#SubCategoryID');
$('#CategoryID').change(function () {
var CategoryID = $('#categoryID').val();
if (!isNaN(CategoryID)) {
var ddCategory = $("#SubCategoryID");
ddCategory.empty();
ddCategory.append($("<option></option>").val("").html("Sub Category!"));
$.ajax({
type: 'GET',
url: '@Url.Action("GetSubCategories", "StoreManager")',
data: { CategoryID: CategoryID },
//dataType: "json",
success: function (data) {
console.log('success',data)//for test
$.each(data, function (i, val) {
ddCategory.append(
//$SubCategoryID.append(
$('<option></option>').val(val.SubCategoryId).html(val.SubCategoryName)
);
});
},
error: function () {
alert("Error");
}
});
}
});
});
代码处理此请求是:
[HttpGet]
public JsonResult GetSubCategories(string categoryID )
{
List<CategoryToSubCategory> allSubCategory = new List<CategoryToSubCategory>();
int id = 0;
if (int.TryParse(categoryID,out id))
{
using(WebStoreEntities1 db1 = new WebStoreEntities1())
{
allSubCategory = db1.CategoryToSubCategories.Where(a => a.CategoryId.Equals(id)).OrderBy(a => a.SubCategory.SubCategoryName).ToList();
}
}
if (Request.IsAjaxRequest())
{
return new JsonResult
{
Data=allSubCategory,
JsonRequestBehavior=JsonRequestBehavior.AllowGet
};
}
else
{
return new JsonResult
{
Data="error"
};
}
}
类别到子类别模型:
public partial class CategoryToSubCategory
{
public int CategoryToSubId { get; set; }
public int CategoryId { get; set; }
public int SubCategoryId { get; set; }
public Nullable<int> ProductId { get; set; }
public virtual Product Product { get; set; }
public virtual SubCategory SubCategory { get; set; }
}
一切都有效,但在 HTML 中 取而代之的是获取类别名称,我得到了一个 erorr,在控制台中我看到此错误:500 服务器错误:ObjectContext 实例已被释放,不能再用于需要连接的操作。
我需要做什么?
序列化 json 响应时,代码将尝试延迟加载并序列化Product and
子类别"。可以通过使用 Select 语句将查询结果投影到仅包含 SubCategoryId 和 SubCategoryName 的匿名类型中来修复此问题。
该想法将应用于您的GetSubCategories
方法中,例如:
using(WebStoreEntities1 db1 = new WebStoreEntities1())
{
allSubCategory = db1.CategoryToSubCategories
.Where(a => a.CategoryId.Equals(id))
.OrderBy(a => a.SubCategory.SubCategoryName)
.Select(a => new {
SubCategoryId = a.SubCategoryId,
SubCategoryName = a.SubCategory.SubCategoryName })
.ToList();
}
因此,现在您不能再在方法的开头声明allSubCategory
变量,因为它的类型是匿名类型。但是,您可以将方法更改为:
[HttpGet]
public JsonResult GetSubCategories(string categoryID )
{
int id = 0;
if (Request.IsAjaxRequest() && int.TryParse(categoryID,out id))
{
using(WebStoreEntities1 db1 = new WebStoreEntities1())
{
var allSubCategory = db1.CategoryToSubCategories
.Where(a => a.CategoryId.Equals(id))
.OrderBy(a => a.SubCategory.SubCategoryName)
.Select(a => new {
SubCategoryId = a.SubCategoryId,
SubCategoryName = a.SubCategory.SubCategoryName })
.ToList();
return new JsonResult
{
Data=allSubCategory,
JsonRequestBehavior=JsonRequestBehavior.AllowGet
};
}
}
return new JsonResult
{
Data="error",
JsonRequestBehavior=JsonRequestBehavior.AllowGet
};
}