嗨朋友们,我有一个名为的表Console
,其中包含一个名为的属性,即ConsoleName
该表的 ID 和两个名为 Game
“Gamer”的表的外键。我遇到的问题是,当我运行应用程序时,一切正常,我可以插入一切正常,但是在删除 CONSOLE 时,我无法从 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);
}
}
}
Console表的索引页:
@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”,但这也不起作用,只是为了告诉你我做了什么