ASP.NET MVC Ajax Post runs OnComplete even if confirmation fails

I have an Ajax form that allows me to dynamically delete rows from a table using OnComplete AjaxOption. This works great.

The problem is that even if I hit “Cancel” in the confirmation dialog, it still executes the OnComplete JavaScript code. Thus, the form is not published, but it seems that it was made for the user (the row was deleted from the table).

Any ideas? Source code below:

OnComplete JS:

 function fadeDel(id) {
    $("#product" + id).fadeOut(500);
  }

Form Code:

<% using (Ajax.BeginForm("DeleteProduct", "Commerce", new { id = product.Id }, 
    new AjaxOptions 
    { 
      OnSuccess = "fadeDel(" + product.Id + ")", 
      Confirm = "Are you sure you want to delete" + product.Title 
    }, new { id = "frm" + product.Id }))
  { %>
    <%= Html.SubmitImage("Delete", Url.Content("~/content/images/12-em-cross.png"))%>
<%} %>
+5
source share
1 answer

OnSuccess , , . "fadeDel", "fadeDel (5)". Javascript, , fadeDel (5) , submit, , AJAX. , Firebug IE.

OnSuccess, , . - "this", . , Title Name:

<% using (Ajax.BeginForm("DeleteProduct", "Commerce", new { id = product.Id }, 
new AjaxOptions 
{ 
  OnSuccess = "fadeDel", 
  Confirm = "Are you sure you want to delete" + product.Title 
}, new { id = "frm" + product.Id, name = product.Id }))
{ %>
  <input type="submit" name="Delete" />
<%} %>

fadeDel, 'this':

function fadeDel() {
    var id = this.name;
    $("#product" + id).fadeOut(500);
}

- , jQuery.ajax() - OnSuccess .

+7

All Articles