我想通过Product_BLL类中的方法Delete交换db.Products.SingleOrDefault(n => n.ProductID == id),但是错误“无法删除对象,因为在ObjectStateManager中找不到该对象”。
这是我的代码:
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");
}