0

こんにちは友人と呼ばれるテーブルがConsoleあり、その中にこれと呼ばれる1つの属性が含まれています。これは、このテーブルのIDと、「ゲーマー」ConsoleNameと呼ばれる2つのテーブルの外部キーです 。Game私が抱えている問題は、アプリケーションを実行するとすべて正常に動作し、すべて正常に挿入できることですが、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」に設定しましたが、これは機能しませんでした。

4

2 に答える 2

0

これをデバッグモードで実行し、deleteメソッドにステップインしましたか?

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」をこのプロパティに置き換えます。/ * * /はコードにコメントを入れるために使用され、それらの間にあるものはコンパイルされません

于 2012-03-21T23:22:54.743 に答える
0

その音から、 a を削除すると、およびテーブルConsoleのレコードが孤立します(これは、外部キーが強制されている場合には許可されません)。したがって、を削除するには、それを外部キーとして使用して他のレコードを削除するか、カスケード動作を設定してそれを行う必要があります。GameGamerConsole

編集:ただし、投稿したエラーは、パラメーターが欠落していることを示しているようです。削除の場合、1 つのビューで削除と呼ばれる 2 つのアクションを使用する可能性があります。1 つの削除アクションは通常のページ リクエスト (通常は削除を確認するため) であり、もう 1 つはポストバック (質問に投稿したもの) を必要とするものです。

プロセスはおそらくこのようなものになります

Get /consoles/details/1

削除ボタンをクリック

Get /consoles/delete/1

確認または OK を押します (フォームの送信ボタンになります)。

Post /consoles/delete/1

于 2012-03-21T22:46:43.750 に答える