views:

48

answers:

1

Does anyone know how to add a confirmation dialog with an "actionlinkwithimage"?

+3  A: 

You can just add a javascript confirm to your ActionLink like:

<%=Html.ActionLink(a => a.ID, "Go", new { onclick="return confirm('Are you sure?');" })%

EDIT: Not sure if you need to know how to implement an ActionLink with an image but here is a helper function that can add an image to an ActionLink:

public static string ActionLinkImage(this HtmlHelper htmlHelper,
    string imagePath, string altText, string actionName,
    RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
    string replaceText = "ActionLinkImageReplaceMe";
    string linkHtml = System.Web.Mvc.Html.LinkExtensions.ActionLink(htmlHelper, 
        replaceText, actionName, routeValues,htmlAttributes);
    return linkHtml.Replace(replaceText,
        String.Format("<img src='{0}' alt='{1}'/>", imagePath, altText));
}
Kelsey
@ Kelsey -- how would I trigger a confirmation from server side? My "html.ActionLinkWithImage" calls an action from the controller, which I would like to have a confirmation before executing the code within the action. How would I go about this?
hersh
@hersh do you need to check something server side to do the confirmation? You could do an ajax post call to a confirmation action in your script and then prompt the `confirm` if necessary. If you want to stick to all server side call then you are going to need an intermediate step to do the server side confirm and display a view etc... then that page would post the completion. This is a bit more complicated because you need to store the posted data over this confirm page.
Kelsey