Skip to content

Two Easy Methods for Confirm Dialog with ActionLink

Hi guys, today I would like to present two easy ways in which to present a confirm dialog to the user (such as “Are you sure you wish to delete this item?”) from a button or link click before performing a particular action on your ASP.NET MVC website.  If you are already somewhat familiar with the standard JavaScript function, confirm(), parts of the process below should not be too foreign.

In both examples I will use the ASP.NET MVC ActionLink control? (for lack of a better words) but it could easily be applied to a RouteLink or any HTML control which supports onClick really!

OPTION 1

Being a fairly standard JavaScript onClick implementation of which the confirmation dialog handler code is appended to the htmlAttributes property of your ActionLink.

[sourcecode language='html']
<%=Html.ActionLink(c => c.Delete(model.ID), “delete”, new { onclick = “return confirm(‘Are you sure you wish to delete this module?’);” })%>

Please Note: I am using the ASP.NET MVC Futures Release which is why you may not be familiar with the strongly typed ActionLink (where the model is specified as a type parameter). But never fear, if yours is slightly different, the important part is making sure the part of the "new" is placed in your htmlAttributes property of your ActionLink.

OPTION 2

My preferred method actually, as it utilises an unobtrusive JavaScript approach (an emerging good practice), does not tie the JavaScript code down with individual links (which is great if you repeat the delete link many times, eg. in a grid) and it uses jQuery.

Just place the script at the top of your page or with other jQuery logic and all you need to tie the reference of the confirm dialog to the button is ensuring the class attributes match.  Using a class attribute I feel is good as you can then repeat this many times using the one class and your JavaScript confirm logic is also not repeated, cutting down code size.

[sourcecode language='html']

$( function() {
$('.button-delete').click(function() {
return confirm('Are you sure you wish to delete this item?');
});
});

{...}

<%=Html.ActionLink(c => c.Delete(model.ID), "delete", new { @class = "button-delete" })%>

Be sure to rename the obvious named parts and away you go!

VN:F [1.9.1_1087]
Rating: 4.1/5 (8 votes cast)
VN:F [1.9.1_1087]
Rating: +3 (from 3 votes)
Two Easy Methods for Confirm Dialog with ActionLink, 4.1 out of 5 based on 8 ratings
Bookmark and Share
kick it on DotNetKicks.com
Shout it

NOW, FOR A WORD FROM OUR SPONSORS

4 Comments

  1. [...] Two Easy Methods for Confirm Dialog with ActionLink [...]

    VA:F [1.9.1_1087]
    Rating: 4.0/5 (1 vote cast)
    VA:F [1.9.1_1087]
    Rating: +1 (from 1 vote)
    Posted on 29-Apr-09 at 3:51 am | Permalink
  2. Thanks for this! ;-)

    VA:F [1.9.1_1087]
    Rating: 5.0/5 (1 vote cast)
    VA:F [1.9.1_1087]
    Rating: +3 (from 3 votes)
    Posted on 14-Jul-09 at 3:06 am | Permalink
  3. nahid

    Thanks ! It works for me.

    VA:F [1.9.1_1087]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.1_1087]
    Rating: 0 (from 0 votes)
    Posted on 08-Dec-09 at 2:26 pm | Permalink
  4. Thanks Graham, this is super useful.

    VA:F [1.9.1_1087]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.1_1087]
    Rating: 0 (from 0 votes)
    Posted on 24-Mar-10 at 5:27 pm | Permalink

One Trackback/Pingback

  1. Interesting Finds: 2009-03-18 - Bolik on 19-Mar-09 at 1:13 am

    [...] Two Easy Methods for Confirm Dialog with ActionLink [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*
My name is Graham O'Neale and I'm a software architect from Gold Coast, Australia. I am an overtime thinker, full time coder and awake part time in the real world. I have a keen interest in software development, particularly in the realm of programming (C#, ASP.NET, ASP.NET MVC, LINQ (2 SQL), Entity Framework, Silverlight, Blend, WCF, WPF) and a keen interest in the cutting edge and innovation. I have a new found love for design patterns, ALT.NET practices and well crafted software architecture. The purpose of this blog is to express any thoughts, findings, tips and gripes along my travels in the wonderful world of coding and technology...