使用确认对话框删除ActionLink

时间:2011-01-13 15:57:54

标签: asp.net-mvc actionlink

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

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

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

11 个答案:

答案 0 :(得分:201)

请勿将routeValueshtmlAttributes混淆。您可能需要this overload

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

答案 1 :(得分:14)

这些是您传递的路线

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

答案 2 :(得分: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确认框。

答案 3 :(得分:2)

您还可以通过将删除项目与消息一起传递来自定义。 在我使用MVC和Razor的情况下,我可以这样做:

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

答案 4 :(得分:2)

试试这个:

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

答案 5 :(得分:2)

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

enter image description here

 <script>
   $(document).ready(function(){

    $('ul li a').each(function(index,item) {
       //color = "#"+Math.floor(Math.random()*16777215).toString(16);
       //select = "ul > li:nth-child("+i+") > a";
       color  = getRandomColor();
       $(item).css('color',color);
    });

   });

   function getRandomColor() {
      var letters = '0123456789ABCDEF';
      var color = '#';
      for (var i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)];
      }
      return color;
     }
</script>

答案 6 :(得分: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>

答案 7 :(得分: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

答案 8 :(得分:0)

enter image description here 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()))

答案 9 :(得分:-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>

答案 10 :(得分:-2)

您也可以尝试使用Html.ActionLink DeleteId