嗨,朋友我有一張名爲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」,但這不起作用,或者只是爲了告訴您我知道我做了什麼
我不認爲是這種情況,因爲我已經嘗試過,我已經刪除了所有依賴項並試圖刪除父項(控制檯)但它仍然不會刪除。 – user1137472 2012-03-21 22:51:42