我在我的 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、ASP和VB.NET 的新手,所以如果我在这里犯了一些新手错误,请原谅我......