2

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

John John
  • 1
  • 72
  • 238
  • 501

1 Answers1

4

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.

Community
  • 1
  • 1
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • i am not using the AjaxOption.Confirm because i want to use customized java script confirm . second thing the deletion will be executed even if the JConfirm dialog is still displayed before selecting Ok or Cancel that why i do not think that it is related to returning false or not ....... – John John Mar 10 '12 at 19:36
  • Try it with the built in `confirm` js function because it should work. It seams `jConfirm` does the confirmation async that's why the return value seems useless. Could you provide a link for `jConfirm`? – nemesv Mar 10 '12 at 19:46
  • thanks for ur reply; here is the link http://www.abeautifulsite.net/blog/2008/12/jquery-alert-dialogs/. regarding providing the built in confirm , i do not knwo from where i should call the buil in script in my ajax.actionliknk – John John Mar 10 '12 at 20:10
  • I've updated my answer it seems `OnBegin` and the `jConfirm` plugin is not compatible... – nemesv Mar 10 '12 at 20:42
  • thanks for you reply, so i will settle by using the ajaxoption.confirm instead , thnx – John John Mar 10 '12 at 22:47