使用确认对话框删除ActionLink

103

我正在尝试实现一个简单的ActionLink,用于使用ASP.NET MVC删除记录。目前为止,这是我的代码:

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

然而,它没有显示确认框。显然我漏掉了一些东西或者链接构造不正确。有人能帮忙吗?

11个回答

219

不要混淆routeValueshtmlAttributes。你可能需要使用这个重载

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

18
不要在GET请求中删除记录!https://dev59.com/EXRA5IYBdhLWcg3w6SRH - user1068352
7
实现不佳。不要在GET请求中修改服务器数据。 - Dan Hunex
肯定应该是:new { id = item.storyId, onclick = "return confirm('您确定要删除这篇文章吗?');" } - Ravendarksky
如果我们不应该使用get来删除,那么如何通过Action链接使用POST来删除? - Unbreakable
1
如果确认是“是”,则调用$.Ajax? - Kiquenet

15

这些是你传递进去的路由

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

你所寻找的重载方法是这个:

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

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


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

以上代码仅适用于 Html.ActionLink。

对于

Ajax.ActionLink

请使用以下代码:

<%= 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" })%>

'确认'选项指定JavaScript确认框。


5
您还可以通过传递删除项以及消息来自定义。在我的情况下,我使用MVC和Razor,所以我可以这样做:
@Html.ActionLink("Delete", 
    "DeleteTag", new { id = t.IDTag }, 
    new { onclick = "return confirm('Do you really want to delete the tag " + @t.Tag + "?')" })

最简单的方法是如何本地化确认消息? - DanDan

4

尝试这个:

<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>

3
使用WebGrid,您可以在这里找到相关信息。操作链接可以如下所示。

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()
                )

1

带有图片和确认删除功能,在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>

1

我也希望能在我的详细视图上有一个删除按钮。最终我意识到需要从该视图中进行发布:

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

而在控制器中:

 // 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

0

在此输入图像描述 MVC5 带有删除对话框和 glyphicon。可能适用于早期版本。

@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()))

-1

在更新/编辑/删除记录之前的任何单击事件都会向用户发出消息框警报,如果选择“确定”,则继续执行该操作,否则保持不变。对于这个代码,不需要编写单独的JavaScript代码,它适用于我。

<a asp-action="Delete" asp-route-ID="@Item.ArtistID" onclick = "return confirm('您确定要删除此艺术家吗?');">删除</a>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接