Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 数据库上下文已在部分视图中释放_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 数据库上下文已在部分视图中释放

Asp.net mvc 数据库上下文已在部分视图中释放,asp.net-mvc,Asp.net Mvc,尝试向部分视图显示IEnumerable项时,我发现db已被释放错误。请检查底部附带的错误屏幕截图。如果你有任何问题,请提问。提前谢谢 视图: @model IEnumerable @foreach(模型中的var项目) { @项目.标题 } 控制器: [ChildActionOnly] [Authorize] public PartialViewResult _UserBookmark(string id) { usin

尝试向部分视图显示IEnumerable项时,我发现db已被释放错误。请检查底部附带的错误屏幕截图。如果你有任何问题,请提问。提前谢谢

视图:

@model IEnumerable
@foreach(模型中的var项目)
{
@项目.标题
}
控制器:

[ChildActionOnly]
        [Authorize]
        public PartialViewResult _UserBookmark(string id)
        {
            using (BlexzWebDbEntities db = new BlexzWebDbEntities())
            {
                int userId = db.Users.Where(x => x.Email == id).FirstOrDefault().UserId;
                IEnumerable<int> ProductIds = db.Bookmarks.Where(x => x.UserId == userId).Select(x => x.ProductId);
                IEnumerable<Product> ListOfProducts = db.Products.Where(x => ProductIds.Contains(x.ProductId));

                return PartialView("_UserBookmark",ListOfProducts);
            }

        }
[ChildActionOnly]
[授权]
public PartialViewResult\u UserBookmark(字符串id)
{
使用(BlexzWebDbEntities db=new BlexzWebDbEntities())
{
int userId=db.Users.Where(x=>x.Email==id).FirstOrDefault().userId;
IEnumerable ProductIds=db.Bookmarks.Where(x=>x.UserId==UserId)。选择(x=>x.ProductId);
IEnumerable ListOfProducts=db.Products.Where(x=>ProductIds.Contains(x.ProductId));
返回PartialView(“用户书签”,产品列表);
}
}

在控制器内编辑下面的行,我认为这可以工作!但是,如果这仍然不起作用,请发表评论

IEnumerable<Product> ListOfProducts = db.Products.Where(x => ProductIds.Contains(x.ProductId)).ToList();
IEnumerable ListOfProducts=db.Products.Where(x=>ProductIds.Contains(x.ProductId)).ToList();
阅读并展示您的尝试。只需
.ToList()
即可解决此问题,请参见复制。使用(BlexzWebDbEntities db=new BlexzWebDbEntities())代码行删除
,只需使用
var db=new BlexzWebDbEntities()
IEnumerable<Product> ListOfProducts = db.Products.Where(x => ProductIds.Contains(x.ProductId)).ToList();