Delete ActionLink with confirm dialog

Cameron picture Cameron · Jan 13, 2011 · Viewed 138.9k times · Source

I'm trying to implement a simple ActionLink that will delete records using ASP.NET MVC. This is what I have so far:

<%= Html.ActionLink("Delete", 
                    "Delete", 
                    new { id = item.storyId, 
                          onclick = "return confirm('Are you sure?');" 
                        })%> 

However, it doesn't show the confirm box. Clearly I'm missing something or I have incorrectly built the link. Can anyone help?

Answer

Darin Dimitrov picture Darin Dimitrov · Jan 13, 2011

Don't confuse routeValues with htmlAttributes. You probably want this overload:

<%= Html.ActionLink(
    "Delete", 
    "Delete", 
    new { id = item.storyId }, 
    new { onclick = "return confirm('Are you sure you wish to delete this article?');" }) 
%>