Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Edit : Here's how to do it with ajax using an HttpPost.</p> <pre><code>// // POST: /Divisions/Delete [HttpPost, ActionName("Delete"), Authorize] public ActionResult DeleteConfirmed(int id) { Division division = _db.Divisions.Single(x =&gt; x.DivisionId == id); string errorMessage; if (DbRelationEnforcer.CanDelete(_db, division, out errorMessage)) { division.SetDeleted(User.Identity.Name); _db.SaveChanges(); return Json(new JsonResponseCreatePartial { Success = true }, JsonRequestBehavior.AllowGet); } return Json(new JsonResponseCreatePartial { Success = false, Message = errorMessage }, JsonRequestBehavior.AllowGet); } </code></pre> <p>Then, on the view, you must use the <code>&lt;input type="submit"&gt;Save changes&lt;/input&gt;</code> to save your changes (within the form), and a simple link/button to delete, like this:</p> <pre><code>&lt;h2&gt;Edit SAS Program&lt;/h2&gt; @using (Html.BeginForm("Edit", "SasProgram", FormMethod.Post)) { &lt;label for="Name"&gt;Name&lt;/label&gt; @Html.TextBoxFor(model =&gt; model.Name) &lt;input id='delete-btn' type="button" class="button" value="Delete" /&gt; &lt;input type="submit" class="button" value="Save Changes" /&gt; } </code></pre> <p>Finally, you have to use JS to post to your action from the view, when the user clicks on Delete.</p> <pre><code>&lt;script type='text/javascript'&gt; $(function() { $("input#delete-btn").click(function(){ $.post('@Url.Action("Delete")', '@Model.Id', function(data) { if(data.Success) { ' ... handle the success case } else { ' ... error management } }); }); }); &lt;/script&gt; </code></pre> <p>This will work, but in order to have a better UX, it would be preferable to have the Delete button from the Index/list view, and using a JQuery UI dialog to confirm before doing the ajax post. This will skip having to load the Edit page if/when you want to delete multiple items one after the other.</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