0

我在我的 MVC 应用程序(用于对数据库进行基本 CRUD 更改)中遇到问题,其中传递给我的“删除”post 方法的对象参数具有空白参数,即使 get 方法返回的对象是正确的。结果,当我尝试从数据库中删除对象时,出现以下异常:

A first chance exception of type 'System.ArgumentNullException' occurred in EntityFramework.dll
System.ArgumentNullException: Value cannot be null.
Parameter name: entity
   at System.Data.Entity.ModelConfiguration.Utilities.RuntimeFailureMethods.Requires(Boolean condition, String userMessage, String conditionText)
   at System.Data.Entity.DbSet`1.Remove(TEntity entity)
   at MvcApplication1.HomeController.Delete(ISBN bookToDelete) in C:\Users\MMcCrimmon\Documents\Visual Studio 2012\Projects\MortonPublishingDatabaseInterface\MvcApplication1\Controllers\HomeController.vb:line 103

经过一番挖掘,我发现了这个问题:Missing something in "Delete" post method in MVC (EF 4.1),这似乎是相同的,但那里的答案并没有解决我的问题。我在这里做错了什么?

这是我的控制器代码:

' GET: /Home/Delete/5

Function Delete(ByVal id As String) As ActionResult
    Dim bookToDelete = _db.ISBNs.Find(id)
    Return View(bookToDelete)
End Function

' POST: /Home/Delete/5

<HttpPost()> _
Function Delete(bookToDelete As ISBN) As ActionResult
    Dim originalBook = _db.ISBNs.Find(bookToDelete.Product_Code)
    Try
        _db.ISBNs.Remove(originalBook)
        _db.SaveChanges()

        Return RedirectToAction("Index")
    Catch exc As Exception
        Return View(originalBook)
    End Try
End Function

这是删除页面的视图:

@ModelType MvcApplication1.ISBN

@Code
    ViewData("Title") = "Delete"
    Layout = "~/Views/Shared/_Layout.vbhtml"
End Code

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>
<p/>
<fieldset>
    <legend>ISBN</legend>

    <div class="display-label">
        <strong>@Html.DisplayNameFor(Function(model) model.Product_Code)</strong>
    </div>
    <div class="display-field">
        @Html.DisplayFor(Function(model) model.Product_Code)
    </div>

    <div class="display-label">
        <strong>@Html.DisplayNameFor(Function(model) model.Title)</strong>
    </div>
    <div class="display-field">
        @Html.DisplayFor(Function(model) model.Title)
    </div>

    <div class="display-label">
        <strong>@Html.DisplayNameFor(Function(model) model.ISBN_UPC)</strong>
    </div>
    <div class="display-field">
        @Html.DisplayFor(Function(model) model.ISBN_UPC)
    </div>

    <div class="display-label">
        <strong>@Html.DisplayNameFor(Function(model) model.ManualName)</strong>
    </div>
    <div class="display-field">
        @Html.DisplayFor(Function(model) model.ManualName)
    </div>

    <div class="display-label">
        <strong>@Html.DisplayNameFor(Function(model) model.PgCount)</strong>
    </div>
    <div class="display-field">
        @Html.DisplayFor(Function(model) model.PgCount)
    </div>

    <div class="display-label">
        <strong>@Html.DisplayNameFor(Function(model) model.BookSize)</strong>
    </div>
    <div class="display-field">
        @Html.DisplayFor(Function(model) model.BookSize)
    </div>

    <div class="display-label">
        <strong>@Html.DisplayNameFor(Function(model) model.ColorOrBW)</strong>
    </div>
    <div class="display-field">
        @Html.DisplayFor(Function(model) model.ColorOrBW)
    </div>

    <div class="display-label">
        <strong>@Html.DisplayNameFor(Function(model) model.PageCount)</strong>
    </div>
    <div class="display-field">
        @Html.DisplayFor(Function(model) model.PageCount)
    </div>
</fieldset>
@Using Html.BeginForm()
    @Html.AntiForgeryToken()
    @<p>
        <input type="submit" value="Delete" /> |
        @Html.ActionLink("Back to List", "Index")
    </p>
End Using

在 MVC (EF 4.1) 中的 "Delete" post method 中丢失某些东西的答案之后,这是修改后的视图(仍然不起作用):

@ModelType MvcApplication1.ISBN

@Code
    ViewData("Title") = "Delete"
    Layout = "~/Views/Shared/_Layout.vbhtml"
End Code

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>
<p/>
@Using Html.BeginForm()
    @Html.AntiForgeryToken()
    @<fieldset>
        <legend>ISBN</legend>

        <div class="display-label">
            <strong>@Html.DisplayNameFor(Function(model) model.Product_Code)</strong>
        </div>
        <div class="display-field">
            @Html.DisplayFor(Function(model) model.Product_Code)
        </div>

        <div class="display-label">
            <strong>@Html.DisplayNameFor(Function(model) model.Title)</strong>
        </div>
        <div class="display-field">
            @Html.DisplayFor(Function(model) model.Title)
        </div>

        <div class="display-label">
            <strong>@Html.DisplayNameFor(Function(model) model.ISBN_UPC)</strong>
        </div>
        <div class="display-field">
            @Html.DisplayFor(Function(model) model.ISBN_UPC)
        </div>

        <div class="display-label">
            <strong>@Html.DisplayNameFor(Function(model) model.ManualName)</strong>
        </div>
        <div class="display-field">
            @Html.DisplayFor(Function(model) model.ManualName)
        </div>

        <div class="display-label">
            <strong>@Html.DisplayNameFor(Function(model) model.PgCount)</strong>
        </div>
        <div class="display-field">
            @Html.DisplayFor(Function(model) model.PgCount)
        </div>

        <div class="display-label">
            <strong>@Html.DisplayNameFor(Function(model) model.BookSize)</strong>
        </div>
        <div class="display-field">
            @Html.DisplayFor(Function(model) model.BookSize)
        </div>

        <div class="display-label">
            <strong>@Html.DisplayNameFor(Function(model) model.ColorOrBW)</strong>
        </div>
        <div class="display-field">
            @Html.DisplayFor(Function(model) model.ColorOrBW)
        </div>

        <div class="display-label">
            <strong>@Html.DisplayNameFor(Function(model) model.PageCount)</strong>
        </div>
        <div class="display-field">
            @Html.DisplayFor(Function(model) model.PageCount)
        </div>
    </fieldset>
    @<p>
        <input type="submit" value="Delete" /> |
        @Html.ActionLink("Back to List", "Index")
    </p>
End Using

完全披露:我是 MVC、ASPVB.NET 的新手,所以如果我在这里犯了一些新手错误,请原谅我......

4

2 回答 2

1

您似乎没有将“bookToDelete”参数传递给 DELETE 方法。事实上,你不应该通过它,因为它是一个对象而不是一个简单的值。我会在删除帖子操作时传递“图书 ID”(与删除获取操作相同)

通过在 @Using Html.BeginForm() 和 End Using 行之间添加以下行来更改删除视图表单代码

@Html.HiddenFor(model => model.id)

并将控制器中的 DELETE post 方法更改为

Function Delete(id as string) As ActionResult

Dim originalBook = _db.ISBNs.Find(id)

于 2013-08-27T00:52:20.530 回答
0

您需要在表单中添加一个指示要传递给控制器​​的参数。例如,如果您的控制器使用“BookToDeleteID”作为参数名称,您的视图 html 应如下所示:

<form>
    ......
    <input type="hidden" name="BookToDeleteID" value="A_NUMBER"/>
    <input type="submit" value="delete"/>
</form>

有时它会出现一些奇怪的情况,您必须将 JSON 对象传递给控制器​​。也许这是 Microsoft 的数据完整性问题。所以我猜你必须为所有动作解析整个模型。

于 2013-08-27T00:59:26.363 回答