Hi friends I have a table called Console
and inside it contains one attribute called ConsoleName
this is the ID of this table and the foreign key of two tables called Game
and `Gamer. The problem that I am having is when I run the application everything works fine and I can insert all fine but when it comes to deleting a CONSOLE I cant delete any record when from the Console table as it gives me the following error:
Value cannot be null.
Parameter name: entity
I am using MVC3 C#
I tried the FOLLOWING:
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(string id?)
{
ConsoleTBL consoletbl = db.ConsoleTBLs.Find(id?);
db.ConsoleTBLs.Remove(consoletbl);
db.SaveChanges();
return RedirectToAction("Index");
}
But it does not work.
Please if you need anything let me know an I shall post what you ask for thanks
EDIT: Console Controller:
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);
}
}
}
Index page of Console table:
@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>
I have set the primary key on the actionlinks to "ConsoleID" but this did not work either just to tell you know what I have done