Note that there are some explanatory texts on larger screens.

plurals
  1. POParameter for "Delete" post method has blank attributes in ASP.NET MVC
    text
    copied!<p>I'm having an issue in my MVC application (for making basic CRUD changes to a database) where the object parameter getting passed to my "Delete" post method has blank parameters, even though the object returned by the get method is correct. As a result, when I try to delete the object from the database I get the following exception:</p> <pre><code>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 </code></pre> <p>After a bit of digging I found this question: <a href="https://stackoverflow.com/questions/9485131/missing-something-in-delete-post-method-in-mvc-ef-4-1">Missing something in &quot;Delete&quot; post method in MVC (EF 4.1)</a>, which seems to be identical, but the answers there did not resolve my problem. What am I doing wrong here?</p> <p>Here's my controller code:</p> <pre class="lang-vb prettyprint-override"><code>' 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 &lt;HttpPost()&gt; _ 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 </code></pre> <p>and here's the view for the Delete page:</p> <pre class="lang-razor prettyprint-override"><code>@ModelType MvcApplication1.ISBN @Code ViewData("Title") = "Delete" Layout = "~/Views/Shared/_Layout.vbhtml" End Code &lt;h2&gt;Delete&lt;/h2&gt; &lt;h3&gt;Are you sure you want to delete this?&lt;/h3&gt; &lt;p/&gt; &lt;fieldset&gt; &lt;legend&gt;ISBN&lt;/legend&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.Product_Code)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.Product_Code) &lt;/div&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.Title)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.Title) &lt;/div&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.ISBN_UPC)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.ISBN_UPC) &lt;/div&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.ManualName)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.ManualName) &lt;/div&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.PgCount)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.PgCount) &lt;/div&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.BookSize)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.BookSize) &lt;/div&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.ColorOrBW)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.ColorOrBW) &lt;/div&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.PageCount)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.PageCount) &lt;/div&gt; &lt;/fieldset&gt; @Using Html.BeginForm() @Html.AntiForgeryToken() @&lt;p&gt; &lt;input type="submit" value="Delete" /&gt; | @Html.ActionLink("Back to List", "Index") &lt;/p&gt; End Using </code></pre> <p>Following the answer for <a href="https://stackoverflow.com/questions/9485131/missing-something-in-delete-post-method-in-mvc-ef-4-1">Missing something in &quot;Delete&quot; post method in MVC (EF 4.1)</a>, here's the modified view (still doesn't work):</p> <pre class="lang-razor prettyprint-override"><code>@ModelType MvcApplication1.ISBN @Code ViewData("Title") = "Delete" Layout = "~/Views/Shared/_Layout.vbhtml" End Code &lt;h2&gt;Delete&lt;/h2&gt; &lt;h3&gt;Are you sure you want to delete this?&lt;/h3&gt; &lt;p/&gt; @Using Html.BeginForm() @Html.AntiForgeryToken() @&lt;fieldset&gt; &lt;legend&gt;ISBN&lt;/legend&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.Product_Code)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.Product_Code) &lt;/div&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.Title)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.Title) &lt;/div&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.ISBN_UPC)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.ISBN_UPC) &lt;/div&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.ManualName)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.ManualName) &lt;/div&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.PgCount)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.PgCount) &lt;/div&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.BookSize)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.BookSize) &lt;/div&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.ColorOrBW)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.ColorOrBW) &lt;/div&gt; &lt;div class="display-label"&gt; &lt;strong&gt;@Html.DisplayNameFor(Function(model) model.PageCount)&lt;/strong&gt; &lt;/div&gt; &lt;div class="display-field"&gt; @Html.DisplayFor(Function(model) model.PageCount) &lt;/div&gt; &lt;/fieldset&gt; @&lt;p&gt; &lt;input type="submit" value="Delete" /&gt; | @Html.ActionLink("Back to List", "Index") &lt;/p&gt; End Using </code></pre> <p>Full disclosure: I'm new to MVC, ASP, <em>and</em> VB.NET, so please forgive me if I'm making some rookie mistake here...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload