错误:无法删除对象,因为在ObjectStateManager

时间:2018-03-08 16:58:30

标签: c#

我想通过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");
    }

0 个答案:

没有答案