I need to fetch something from the database and return that with the model if ModelState
is not valid, however I seem to not be able to use using
and I want to.
So this code works:
[HttpPost]
public ActionResult EditProduct(ProductVM productVM, HttpPostedFileBase file)
{
// Check model state
if (!ModelState.IsValid)
{
Db db = new Db();
productVM.Categories = new SelectList(db.Categories, "Id", "Name");
return View(productVM);
}
return View(productVM);
}
And this code throws the following error:
The operation cannot be completed because the DbContext has been disposed.
[HttpPost]
public ActionResult EditProduct(ProductVM productVM, HttpPostedFileBase file)
{
// Check model state
if (!ModelState.IsValid)
{
using (Db db = new Db())
{
//Db db = new Db();
productVM.Categories = new SelectList(db.Categories, "Id", "Name");
}
return View(productVM);
}
return View(productVM);
}
Can I somehow use using
and still have it work?