Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Actionlink with confirmation dialog

Can I show a confirmation message for an ActionLink?

Do I need to use javascript? Is it possible without it?

Could you give some example for me?

Thank you.

//I want to make a confirmation message appear before the link opens.
@Html.ActionLink("Checkout and view order list", "Order", "Order")
like image 596
wholee1 Avatar asked Apr 17 '12 19:04

wholee1


1 Answers

Using the overload Html.ActionLink(string linkText, string actionName, string controllerName, object RouteValues, object HtmlAttributes) and some javascript, you can do the following:

@Html.ActionLink("Checkout and view order list", "Order", "Order", null, new { onclick="return confirm('Are you sure you want to click this link?')" })

This will add the HTML attribute onclick, which will execute the specified javascript when the link is clicked. If an onclick event on a link (or a form's submit button) returns false, the action (following the link, posting the form) doesn't happen. The confirm(message) function shows the user a confirmation dialog with the specified message, and returns either true or false depending on the user's response.

like image 164
yoozer8 Avatar answered Sep 28 '22 18:09

yoozer8