使用确认对话框删除ActionLink

Cam*_*ron 97 asp.net-mvc actionlink

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

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

但是,它不会显示确认框.很明显我错过了一些东西,或者我错误地构建了链接.有人可以帮忙吗?

Dar*_*rov 207

不要混淆routeValueshtmlAttributes.你可能想要这个重载:

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

  • 避免在GET请求时删除记录!http://stackoverflow.com/questions/786070/why-should-you-delete-using-an-http-post-or-delete-rather-than-get (16认同)
  • 执行不力.不要在GET上修改服务器数据 (6认同)

hun*_*ter 15

那些是你传递的路线

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

您正在寻找的重载方法是这样的:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    Object routeValues,
    Object htmlAttributes
)
Run Code Online (Sandbox Code Playgroud)

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


T G*_*pta 14

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

以上代码仅适用于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" })%>
Run Code Online (Sandbox Code Playgroud)

"确认"选项指定javascript确认框.


Nic*_*ahn 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 + "?')" })
Run Code Online (Sandbox Code Playgroud)