使用确认对话框删除ActionLink


98

我正在尝试实现一个简单的方法ActionLink,该方法将使用ASP.NET MVC删除记录。这是我到目前为止所拥有的:

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

但是,它没有显示确认框。显然我遗漏了一些东西,或者我没有正确构建链接。有人可以帮忙吗?

Answers:


211

不要routeValues与混淆htmlAttributes。您可能想要此重载

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

16
避免在GET请求后删除记录!stackoverflow.com/questions/786070/...
user1068352

6
实施不力。不要修改获取服务器数据
丹HUNEX

当然应该是:new {id = item.storyId,onclick =“返回确认('您确定要删除此文章吗?');” })
Ravendarksky's

如果我们不打算使用get删除,那么如何使用POST通过Action链接?
牢不可破

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确认框。


4

您还可以通过传递删除项和消息来自定义。就我而言,使用MVC和Razor,所以我可以这样做:

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

3

试试这个 :

<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,您可以在这里找到它,操作链接如下所示。

在此处输入图片说明

    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

在此处输入图片说明带有删除对话框和Glyphicon的MVC5。可以使用以前的版本。

@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

更新/编辑/删除记录之前的任何单击事件消息框都会警告用户,如果继续执行操作“确定”,否则“取消”保持不变。对于此代码,无需对单独的Java脚本代码进行修改。这个对我有用

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


-2

您也可以为Html.ActionLink DeleteId尝试此操作


3
你能解释一下这个答案吗?也许提供一个代码片段来说明您的建议,或者描述应该在OP的代码中放哪里?
skrrgwasme 2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.