ASP.NET MVC3 Ajax.ActionLink - Conditional Confirmation Dialog Box

I have @ Ajax.ActionLink for which I would like to display a confirmation dialog only if certain conditions are met (the user has unsaved changes). I created a javascript function that displays a confirmation dialog as needed and returns true or false based on the response. I attached it to the onclick ActionLink event, but a false result does not cancel the action. Here is an example of my code:

@Ajax.ActionLink("Done", .. , .. , new AjaxOptions() { UpdateTargetId = "MyContainerId"}, new { onclick = "ConfirmDone()" }) 

Here's the javascript function

 function ConfirmDone() { //for testing purposes we can always show the dialog box return confirm("Are you sure you want to lose unsaved changes?"); } 

What is the best approach to display a conditional confirmation dialog for Ajax.ActionLink?

+4
source share
2 answers

Use the OnBegin event:

 @Ajax.ActionLink("Done", "ActionName", new AjaxOptions { OnBegin = "return ConfirmDone()", UpdateTargetId = "MyContainerId" }) 

You can also use the Confirm ajax option if all you have to do is open a confirmation window. If you need to do more custom logic (or want to use a custom dialog), you will need to use OnBegin.

Here is an example using Confirm:

 @Ajax.ActionLink("Done", "ActionName", new AjaxOptions { Confirm= "Are you sure you want to do this?", UpdateTargetId = "MyContainerId" }) 
+12
source

I think you just need to change it like this:

 @Ajax.ActionLink("Done", .. , .. , new AjaxOptions() { UpdateTargetId = "MyContainerId"}, new { onclick = "return ConfirmDone();" }) 
-one
source

All Articles