-2

I am getting db has been disposed error when trying to display IEnumerable items to partial view. Please check the error screen shot attached at bottom. If you have any question then plz ask. Thanks in advance.

View:

@model IEnumerable<Blexz.Models.Product>

@foreach(var item in Model)
    {
        <span>@item.Title</span>
    }

Controller:

[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);
            }

        }

Picture1

Kevin himch
  • 287
  • 3
  • 18
  • Read [ask] and show what you have tried. A mere `.ToList()` will fix this issue, see duplicate. – CodeCaster Dec 14 '17 at 09:20
  • Remove the `using (BlexzWebDbEntities db = new BlexzWebDbEntities())` line of code and just use `var db = new BlexzWebDbEntities();` –  Dec 14 '17 at 09:20

1 Answers1

0

Edit line bellow inside your controller, I think this can be working! However comments if this still not works.

IEnumerable<Product> ListOfProducts = db.Products.Where(x => ProductIds.Contains(x.ProductId)).ToList();