2011-01-13 143 views
83

我试图实现一个简单的ActionLink,它将使用ASP.NET MVC删除记录。这是我到目前为止:使用确认对话框删除ActionLink

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

但是,它不显示确认框。很明显,我错过了一些东西,或者我错误地建立了链接。谁能帮忙?

回答

176

不要混淆htmlAttributesrouteValues 。你可能想this overload

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

这是完美的。谢谢。 – Cameron 2011-01-13 16:03:02

+3

真棒先生....我正在寻找很多,并思考达林在哪里..... :) – 2011-12-11 13:56:02

13

那些都是你传递

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

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

-2

你也可以尝试一下本作Html.ActionLink通过传递删除项与消息一起DeleteId

1

您还可以自定义。 在我的情况下,使用MVC和剃刀,所以我可以这样做:

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

试试这个:

<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> 
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> 
0
工作

使用webgrid you can found it here,操作链接可能如下所示。

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

任何click事件之前更新/编辑/删除记录的消息框提醒用户,如果“确定”继续进行其他操作“取消”保持不变。对于这段代码,不需要单独的java脚本代码。它适用于我

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