2010-11-12 51 views
0

// GET:/產品/刪除/ 5刪除所有未在MVC工作2

public ActionResult Delete(int id) 
    { 
     var res = (from r in data.Products where r.ProductID == id select r).FirstOrDefault(); 
     //return View(data.Products.FirstOrDefault(p => p.ProductID == id)); 
     return View(res); 
    } 

    // 
    // POST: /Product/Delete/5 
    // [HttpPost] 
    [AcceptVerbs(HttpVerbs.Post)] 
    **public ActionResult Delete(Product producttodelete)** 
    { 

     try 
     { 
      // TODO: Add delete logic here 
      var res = (from r in data.Products where r.ProductID == producttodelete.ProductID select r).FirstOrDefault(); 
      // var productto = data.Products.Single(p => p.ProductID == producttodelete.ProductID); 
      data.Products.DeleteObject(res); 
      data.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     catch 
     { 
      return View(); 
     } 
    } 
} 

在這裏,在行 「producttodelete」 我沒有得到它比創造,細節卡明null.Rather任何值,編輯工作正常....只刪除不工作.......我試了很多

回答

1

假設你使用強類型的意見,你有沒有嘗試過:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Delete(int id, Product productToDelete) 
{ 
    try 
    { 
     var res = (from r in data.Products where r.ProductID == id select r).FirstOrDefault(); 
     data.Products.DeleteObject(res); 
     data.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
    catch 
    { 
     return View(); 
    } 
} 

...或者,如果你不使用強類型的觀點:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Delete(int id, FormCollection collection) 
{ 
    try 
    { 
     var res = (from r in data.Products where r.ProductID == id select r).FirstOrDefault(); 
     data.Products.DeleteObject(res); 
     data.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
    catch 
    { 
     return View(); 
    } 
} 

在無論哪種情況,您都需要提供一個id參數,然後您可以使用該參數從數據存儲中獲取對象。 MVC2中的默認路由映射(在Global.asax.cs中)將採用帖子URL中的ID並將其映射到此參數。

+0

,而過路data.savechanges我得到這個錯誤( 「在更新條目時發生錯誤,請參閱內部異常以瞭解詳細信息)在catch塊 – 2010-11-12 08:43:59

+0

@Karthik所以什麼是內部異常? – Graham 2010-11-12 08:45:34

+0

public ActionResult Delete(int id) {var res =(from r in data.Products where r .ProductID == id select r).FirstOrDefault(); return View(res);} [AcceptVerbs(HttpVerbs.Post)] public ActionResult Delete(int id,FormCollection collection) { 嘗試 {var res =(from r in data.Products where r.ProductID == id select r).FirstOrDefault(); data.Products.DeleteObject(res); data.SaveChanges(); 返回RedirectToAction(「指數」);} 抓 {返回查看();} } – 2010-11-12 08:57:19

0

爲什麼不只是得到像你在其餘的代碼,而不是試圖獲得整個模型?無論如何,你似乎只是選擇使用ID。試試這個:

 [AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Delete(int id) 
     { 

      try 
      { 
       // TODO: Add delete logic here 
       var res = (from r in data.Products where r.ProductID == id select r).FirstOrDefault(); 
       // var productto = data.Products.Single(p => p.ProductID == producttodelete.ProductID); 
       data.Products.DeleteObject(res); 
       data.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      catch 
      { 
       return View(); 
      } 
     } 
+0

AS根據你回覆兩個函數是相同的它說錯誤「類型'.....'已經定義了一個名爲'Delete'的成員具有相同的參數類型 – 2010-11-12 08:09:04

+0

[AcceptVerbs(HttpVerbs.Post)] public ActionResult刪除(int id,FormCollection集合) – hazimdikenli 2010-11-12 08:30:31

+0

public ActionResult Delete(int id) {var res =(from r in data.Products where r.ProductID == id select r).FirstOrDefault(); return View(res);} 的[AcceptVerbs(HttpVerbs.Post)] 公共的ActionResult刪除(INT ID,的FormCollection集合) { 嘗試 {風險RES =(從data.Products r其中r.ProductID == ID選擇R).FirstOrDefault( ); data.Products.DeleteObject(res); data.SaveChanges(); 返回RedirectToAction( 「指數」);} 抓 {返回查看();}} 它 – 2010-11-12 08:58:50