日期:2014-05-18  浏览次数:21240 次

MVC的Delete()方法怎么搞?
绿色猪屎的地方有错,没办法删除一条Product
要怎样修改?


C# code
       private MagicShopEntities entities = new MagicShopEntities();

       public ActionResult Delete(int id)
        {
            var productToDelete = (from p in entities.Product
                                 where p.ProductId == id
                                 select p).First();

            return View(productToDelete);
        }

        //
        // POST: /Product/Delete/5


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete([Bind(Exclude = "ProductId")] Product productToDelete)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return View();
                }



                entities.Delete(productToDelete)  //entities不包含Delete的定义。
                  entities.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


------解决方案--------------------
private MagicShopEntities entities = new MagicShopEntities();
你这个是用entity Framework做的?
如果是的话你可以这样做:
var productToDelete = (from p in entities.Product
where p.ProductId == id
select p).First();

return View(productToDelete);
修改为 entities.DeleteObject(productToDelete)

这样就可以删除掉了。