John John
John John

Reputation: 1

Ajax.Actionlink OnBegin confirmation will not prevent the ajax call from being called if the user click on "Cancel" button

i have the following ajax.actionlink:-

@Ajax.ActionLink("Delete", "Delete", "Answer",
    new { id = answer.AnswersID },
      new AjaxOptions
      {
          OnBegin = "deleteconfirmation1",
          HttpMethod = "Post",
          UpdateTargetId = @answer.AnswersID.ToString(),
          OnSuccess = "deleteconfirmation",

      })

and the following deleteconfirmation1.js that should display a confirmation message before executing the ajax call:-

function deleteconfirmation1() {
jConfirm('Deletion Confirmation', 'Are you sure you want to delete this Answer');
}

the problem that i am facing is that the onbegin confiramtion will not prevent the onsuccess script from being executed even if the user click on the cancel button? so how i can make the OnBegin to work as a confirmation stage, so the application will wait for the user selection either "Ok" or "cancel" before processding with the ajax call or not !!! ?? BR

Upvotes: 2

Views: 3553

Answers (1)

nemesv
nemesv

Reputation: 139758

You need return false from the OnBegin method to cancel the request.

Note that Ajax.ActionLink already supports confirmation with the AjaxOptions.Confirm property.

You also need to include ~/Scripts/jquery.unobtrusive-ajax.js in your view/layout to use the @Ajax. helpers.

EDIT issue with jConfirm:

Your problem is that the signature of jConfirm is the following:

void jConfirm(message, [title, callback])

It's instead of returning true/false expects a callback function which is called when the user clicked on ok/cancel.

This causes the problem because you need to return false from the OnBegin but you don't have a return value from jConfirm just in the callback...

So you should wait for the callback.... see this SO question Can you wait for javascript callback? (which is also about jConfirm) for more information.

The conclusion is that you cannot use jConfirm to cancel the request with the OnBegin method.

Upvotes: 4

Related Questions