Edit : Here's how to do it with ajax using an HttpPost.
//
// POST: /Divisions/Delete
[HttpPost, ActionName("Delete"), Authorize]
public ActionResult DeleteConfirmed(int id)
{
Division division = _db.Divisions.Single(x => 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);
}
Then, on the view, you must use the <input type="submit">Save changes</input>
to save your changes (within the form), and a simple link/button to delete, like this:
<h2>Edit SAS Program</h2>
@using (Html.BeginForm("Edit", "SasProgram", FormMethod.Post))
{
<label for="Name">Name</label>
@Html.TextBoxFor(model => model.Name)
<input id='delete-btn' type="button" class="button" value="Delete" />
<input type="submit" class="button" value="Save Changes" />
}
Finally, you have to use JS to post to your action from the view, when the user clicks on Delete.
<script type='text/javascript'>
$(function() {
$("input#delete-btn").click(function(){
$.post('@Url.Action("Delete")', '@Model.Id', function(data) {
if(data.Success) {
' ... handle the success case
} else {
' ... error management
}
});
});
});
</script>
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.