I want to exchange db.Products.SingleOrDefault(n => n.ProductID == id) by method Delete in class Product_BLL but error "The object cannot be deleted because it was not found in the ObjectStateManager".
This is my code:
public ActionResult Delete(int id)
{
if (id.ToString() == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
//public class Product_BLL {
//public static Product Delete(int id)
//{
//LOLShopEntities db = new LOLShopEntities();
//var p = db.Products.SingleOrDefault(n => n.ProductID == id);
//return p;
//}
//}
var p = Product_BLL.Delete(id);
//var p = db.Products.SingleOrDefault(n => n.ProductID == id);
if (p == null)
{
return HttpNotFound();
}
db.Products.Remove(p);
db.SaveChanges();
return RedirectToAction("Index");
}