Note that there are some explanatory texts on larger screens.

plurals
  1. POMissing something in "Delete" post method in MVC (EF 4.1)
    primarykey
    data
    text
    <p>Following this example .. <a href="http://msdn.microsoft.com/en-us/data/gg685489" rel="nofollow">http://msdn.microsoft.com/en-us/data/gg685489</a></p> <p>I am running into issues with Delete functionality.</p> <pre><code>[HttpPost] public ActionResult Delete(int id, Blog blog) { try { using (var db = new BlogDataEntities()) { //Having issue here.. as soon as the next line is run in debug //mode .. it goes to catch.. and returns a view with null values. db.Entry(blog).State = System.Data.EntityState.Deleted; db.SaveChanges(); } return RedirectToAction("Index"); } catch { return View(); } } </code></pre> <p>In the parameters I checked 'blog' does not get the actual blog model that needs to be deleted. All the other methods work fine (Edit, Delete (get)..etc.. but Delete post fails. Am I missing something? thanks in advance for the help.</p> <p>EDIT:</p> <p>view code</p> <pre><code>@model DBFirstMVC.Models.Blog @{ ViewBag.Title = "Delete"; } &lt;h2&gt;Delete&lt;/h2&gt; &lt;h3&gt;Are you sure you want to delete this?&lt;/h3&gt; &lt;fieldset&gt; &lt;legend&gt;Blog&lt;/legend&gt; &lt;div class="display-label"&gt;Title&lt;/div&gt; &lt;div class="display-field"&gt;@Model.Title&lt;/div&gt; &lt;div class="display-label"&gt;BloggerName&lt;/div&gt; &lt;div class="display-field"&gt;@Model.BloggerName&lt;/div&gt; &lt;/fieldset&gt; @using (Html.BeginForm()) { &lt;p&gt; &lt;input type="submit" value="Delete" /&gt; | @Html.ActionLink("Back to List", "Index") &lt;/p&gt; } </code></pre> <p>EDIT 2: Non Razor Code in view:</p> <pre><code>&lt;% using (Html.BeginForm()) { %&gt; &lt;p&gt; &lt;input type="submit" value="Delete" /&gt; | &lt;%: Html.ActionLink("Back to List", "Index") %&gt; &lt;/p&gt; &lt;% } %&gt; </code></pre> <p>EDIT 3: (I tried in aspx)</p> <pre><code>&lt;% using (Html.BeginForm()) { %&gt; &lt;p&gt; &lt;%=Html.DisplayForModel();%&gt; //Tried Html.EditorForModel also.. &lt;input type="submit" value="Delete" /&gt; | &lt;%: Html.ActionLink("Back to List", "Index") %&gt; &lt;/p&gt; &lt;% } %&gt; </code></pre> <p></p> <p>FINAL EDIT (Corrected Solution)</p> <pre><code>@model DBFirstMVC.Models.Blog @{ ViewBag.Title = "Delete"; } &lt;h2&gt;Delete&lt;/h2&gt; &lt;h3&gt;Are you sure you want to delete this?&lt;/h3&gt; @using (Html.BeginForm()) { &lt;p&gt; &lt;fieldset&gt; &lt;legend&gt;Blog&lt;/legend&gt; &lt;div class="display-label"&gt;Title&lt;/div&gt; &lt;div class="display-field"&gt;@Model.Title&lt;/div&gt; &lt;div class="display-label"&gt;BloggerName&lt;/div&gt; &lt;div class="display-field"&gt;@Model.BloggerName&lt;/div&gt; &lt;input type="submit" value="Delete" /&gt; | @Html.ActionLink("Back to List", "Index") &lt;/fieldset&gt; &lt;/p&gt; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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