Delete ActionLink with confirm dialog

asp.net MvcActionlink

asp.net Mvc Problem Overview


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?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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?');" }) 
%>

Solution 2 - asp.net Mvc

those are routes you're passing in

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

The overloaded method you're looking for is this one:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    Object routeValues,
    Object htmlAttributes
)

http://msdn.microsoft.com/en-us/library/dd492124.aspx

Solution 3 - asp.net Mvc

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

The above code only works for Html.ActionLink.

For

> Ajax.ActionLink

use the following code:

<%= Ajax.ActionLink(" ", "deleteMeeting", new { id = Model.eventID, subid = subItem.ID, fordate = forDate, forslot = forslot }, new AjaxOptions
                                            {
                                                Confirm = "Are you sure you wish to delete?",
                                                UpdateTargetId = "Appointments",
                                                HttpMethod = "Get",
                                                InsertionMode = InsertionMode.Replace,
                                                LoadingElementId = "div_loading"
                                            }, new { @class = "DeleteApointmentsforevent" })%>

The 'Confirm' option specifies javascript confirm box.

Solution 4 - asp.net Mvc

You can also customize the by passing the delete item along with the message. In my case using MVC and Razor, so I could do this:

@Html.ActionLink("Delete", 
    "DeleteTag", new { id = t.IDTag }, 
    new { onclick = "return confirm('Do you really want to delete the tag " + @t.Tag + "?')" })

Solution 5 - asp.net Mvc

Try this :

<button> @Html.ActionLink(" ", "DeletePhoto", "PhotoAndVideo", new { id = item.Id }, new { @class = "modal-link1", @OnClick = "return confirm('Are you sure you to delete this Record?');" })</button>

Solution 6 - asp.net Mvc

Using webgrid you can found it here, the action links could look like the following.

enter image description here

    grid.Column(header: "Action", format: (item) => new HtmlString(
                     Html.ActionLink(" ", "Details", new { Id = item.Id }, new { @class = "glyphicon glyphicon-info-sign" }).ToString() + " | " +
                     Html.ActionLink(" ", "Edit", new { Id = item.Id }, new { @class = "glyphicon glyphicon-edit" }).ToString() + " | " +
                     Html.ActionLink(" ", "Delete", new { Id = item.Id }, new { onclick = "return confirm('Are you sure you wish to delete this property?');", @class = "glyphicon glyphicon-trash" }).ToString()
                )

Solution 7 - asp.net Mvc

With image and confirmation on delete, which works on mozilla firefox

<button> @Html.ActionLink(" ", "action", "controller", new { id = item.Id }, new { @class = "modal-link1", @OnClick = "return confirm('Are you sure you to delete this Record?');" })</button>
<style>
a.modal-link{ background: URL(../../../../Content/Images/Delete.png) no-repeat center;
            display: block;
            height: 15px;
            width: 15px;
           
        }
</style>

Solution 8 - asp.net Mvc

I wanted the same thing; a delete button on my Details view. I eventually realised I needed to post from that view:

@using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()
            @Html.HiddenFor(model => model.Id)
            @Html.ActionLink("Edit", "Edit", new { id = Model.Id }, new { @class = "btn btn-primary", @style="margin-right:30px" })
            
            <input type="submit" value="Delete" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this record?');" />
        }

And, in the Controller:

 // this action deletes record - called from the Delete button on Details view
    [HttpPost]
    public ActionResult Details(MainPlus mainPlus)
    {
        if (mainPlus != null)
        {
            try
            {
                using (IDbConnection db = new SqlConnection(PCALConn))
                {
                    var result = db.Execute("DELETE PCAL.Main WHERE Id = @Id", new { Id = mainPlus.Id });
                }
                return RedirectToAction("Calls");
            } etc

Solution 9 - asp.net Mvc

enter image description here MVC5 with delete dialogue & glyphicon. May work previous versions.

@Html.Raw(HttpUtility.HtmlDecode(@Html.ActionLink(" ", "Action", "Controller", new { id = model.id }, new { @class = "glyphicon glyphicon-trash", @OnClick = "return confirm('Are you sure you to delete this Record?');" }).ToHtmlString()))

Solution 10 - asp.net Mvc

Any click event before for update /edit/delete records message box alerts the user and if "Ok" proceed for the action else "cancel" remain unchanged. For this code no need to right separate java script code. it works for me

<a asp-action="Delete" asp-route-ID="@Item.ArtistID" onclick = "return confirm('Are you sure you wish to remove this Artist?');">Delete</a>

Solution 11 - asp.net Mvc

You can also try this for Html.ActionLink DeleteId

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCameronView Question on Stackoverflow
Solution 1 - asp.net MvcDarin DimitrovView Answer on Stackoverflow
Solution 2 - asp.net MvchunterView Answer on Stackoverflow
Solution 3 - asp.net MvcT GuptaView Answer on Stackoverflow
Solution 4 - asp.net MvcNick KahnView Answer on Stackoverflow
Solution 5 - asp.net MvcGirishBabuCView Answer on Stackoverflow
Solution 6 - asp.net MvcHedegoView Answer on Stackoverflow
Solution 7 - asp.net MvcGirishBabuCView Answer on Stackoverflow
Solution 8 - asp.net MvcAntony DView Answer on Stackoverflow
Solution 9 - asp.net MvcTerry HView Answer on Stackoverflow
Solution 10 - asp.net MvcSeeni AbiramiView Answer on Stackoverflow
Solution 11 - asp.net MvcInnocent ZondoView Answer on Stackoverflow