2012-03-21 130 views
0

嗨,朋友我有一張名爲Console的表,其中包含一個名爲ConsoleName的屬性,這是該表的ID和兩個名爲Game和`Gamer的表的外鍵。那我遇到的問題是,當我運行該應用程序的一切工作正常,我可以將所有的罰款,但是當涉及到刪除一個CONSOLE我不能刪除任何記錄時,從控制檯表,因爲它給了我下面的錯誤:無法刪除主鍵表

Value cannot be null. 
Parameter name: entity 

我使用MVC3 C#

我試過如下:

[HttpPost, ActionName("Delete")] 
    public ActionResult DeleteConfirmed(string id?) 
    {    
     ConsoleTBL consoletbl = db.ConsoleTBLs.Find(id?); 
     db.ConsoleTBLs.Remove(consoletbl); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

但它不工作。

如有什麼需要,讓我知道了我會張貼你問什麼感謝

編輯:控制檯控制器:

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.Entity; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using MvcApplication1.Models; 

namespace MvcApplication1.Controllers 
{ 
    public class ConsoleController : Controller 
    { 
     private GameZoneEntities3 db = new GameZoneEntities3(); 

     // 
     // GET: /Console/ 

     public ViewResult Index() 
     { 
      return View(db.ConsoleTBLs.ToList()); 
     } 

     // 
     // GET: /Console/Details/5 

     public ViewResult Details(string id) 
     { 
      ConsoleTBL consoletbl = db.ConsoleTBLs.Find(id); 
      return View(consoletbl); 
     } 

     // 
     // GET: /Console/Create 

     public ActionResult Create() 
     { 
      return View(); 
     } 

     // 
     // POST: /Console/Create 

     [HttpPost] 
     public ActionResult Create(ConsoleTBL consoletbl) 
     { 
      if (ModelState.IsValid) 
      { 
       db.ConsoleTBLs.Add(consoletbl); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      return View(consoletbl); 
     } 

     // 
     // GET: /Console/Edit/5 

     public ActionResult Edit(string id) 
     { 
      ConsoleTBL consoletbl = db.ConsoleTBLs.Find(id); 
      return View(consoletbl); 
     } 

     // 
     // POST: /Console/Edit/5 

     [HttpPost] 
     public ActionResult Edit(ConsoleTBL consoletbl) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Entry(consoletbl).State = EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      return View(consoletbl); 
     } 

     // 
     // GET: /Console/Delete/5 

     public ActionResult Delete(string id) 
     { 
      ConsoleTBL consoletbl = db.ConsoleTBLs.Find(id); 
      return View(consoletbl); 
     } 

     // 
     // POST: /Console/Delete/5 

     [HttpPost, ActionName("Delete")] 
     public ActionResult DeleteConfirmed(string id) 
     {    
      ConsoleTBL consoletbl = db.ConsoleTBLs.Find(id); 
      db.ConsoleTBLs.Remove(consoletbl); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      db.Dispose(); 
      base.Dispose(disposing); 
     } 
    } 
} 

控制檯表的索引頁:

@model IEnumerable<MvcApplication1.Models.ConsoleTBL> 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
<table> 
    <tr> 
     <th> 
      ConsoleID 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.ConsoleID) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) | 
      @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) | 
      @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }) 
     </td> 
    </tr> 
} 

</table> 

我已將操作鏈接上的主鍵設置爲「ConsoleID」,但這不起作用,或者只是爲了告訴您我知道我做了什麼

回答

0

從它的聲音,如果你刪除一個Console你會孤兒的GameGamer表中的記錄(當外鍵是強制執行,未允許的話)。因此,要刪除Console,您必須將其作爲外鍵刪除其他記錄,或設置級聯行爲爲您執行此操作。

編輯:但是,您發佈的錯誤似乎意味着您缺少一個參數。對於刪除,您可能會有兩個操作,稱爲刪除與一個視圖。一個刪除操作將是一個普通的頁面請求(通常是爲了確認刪除),另一個將是需要回發的問題(您在問題中發佈的問題)。

的過程可能會是這樣的

Get /consoles/details/1

點擊刪除按鈕

Get /consoles/delete/1

按下確認鍵或OK(這將是一個表單提交按鈕)

Post /consoles/delete/1

+0

我不認爲是這種情況,因爲我已經嘗試過,我已經刪除了所有依賴項並試圖刪除父項(控制檯)但它仍然不會刪除。 – user1137472 2012-03-21 22:51:42

0

您是否在調試模式下運行此操作並進入刪除方法?

您是否檢查以確保consoletbl存在?

[HttpPost, ActionName("Delete")] 
public ActionResult DeleteConfirmed(string id) 
{    
    ConsoleTBL consoletbl = db.ConsoleTBLs.Find(id); 

    if(consoletbl != null) 
    { 
     db.ConsoleTBLs.Remove(consoletbl); 
     db.SaveChanges(); 
    } 
    else 
    { 
     // should probably return an error message 
     throw new ArgumentNullException("Could not find a console with the id of " + id); 
    } 
    return RedirectToAction("Index"); 
} 

如果找不到您傳入的ID,則應該拋出異常...

@model IEnumerable<MvcApplication1.Models.ConsoleTBL> 

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
<table> 
    <tr> 
     <th> 
      ConsoleID 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.ConsoleID) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { id=item.ConsoleID}) | 
      @Html.ActionLink("Details", "Details", new { id=item.ConsoleID}) | 
      @Html.ActionLink("Delete", "Delete", new { id=item.ConsoleID}) 
     </td> 
    </tr> 
} 

</table> 

我假設ConsoleID是主ID,所以我用這個屬性替換「PrimaryKey」。 /* * /用於在您的代碼中放置註釋,並且它們之間的任何內容都不會被編譯

+0

是的控制檯表存在,是的,我沒有運行它在調試,沒有它並沒有幫助,只要我運行它,並訪問控制檯的索引頁,並嘗試刪除控制檯我得到的錯誤,我已經提到 – user1137472 2012-03-21 23:24:35

+0

我會嘗試你的代碼,並讓你知道結果 – user1137472 2012-03-21 23:25:36

+0

我已經添加了你的代碼,並且有一個變化,當我知道嘗試按刪除時什麼都沒有發生,頁面刷新自己回來,記錄仍然存在,但它不會產生錯誤並崩潰就像它用於 – user1137472 2012-03-21 23:28:00