asp.net-mvc 使用确认对话框删除 ActionLink
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4682107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete ActionLink with confirm dialog
提问by Cameron
I'm trying to implement a simple ActionLink
that will delete records using ASP.NET MVC. This is what I have so far:
我正在尝试实现一个ActionLink
使用 ASP.NET MVC 删除记录的简单方法。这是我到目前为止:
<%= Html.ActionLink("Delete",
"Delete",
new { id = item.storyId,
onclick = "return confirm('Are you sure?');"
})%>
However, it doesn't show the confirm box. Clearly I'm missing something or I have incorrectly built the link. Can anyone help?
但是,它不显示确认框。显然我遗漏了一些东西,或者我错误地建立了链接。任何人都可以帮忙吗?
回答by Darin Dimitrov
Don't confuse routeValues
with htmlAttributes
. You probably want this overload:
不要routeValues
与htmlAttributes
. 你可能想要这个重载:
<%= Html.ActionLink(
"Delete",
"Delete",
new { id = item.storyId },
new { onclick = "return confirm('Are you sure you wish to delete this article?');" })
%>
回答by hunter
those are routes you're passing in
这些是你经过的路线
<%= Html.ActionLink("Delete", "Delete",
new { id = item.storyId },
new { onclick = "return confirm('Are you sure you wish to delete this article?');" }) %>
The overloaded method you're looking for is this one:
您正在寻找的重载方法是这个:
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
Object routeValues,
Object htmlAttributes
)
回答by T Gupta
<%= Html.ActionLink("Delete", "Delete",
new { id = item.storyId },
new { onclick = "return confirm('Are you sure you wish to delete this article?');" }) %>
The above code only works for Html.ActionLink.
以上代码仅适用于 Html.ActionLink。
For
为了
Ajax.ActionLink
Ajax.ActionLink
use the following code:
使用以下代码:
<%= 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" })%>
The 'Confirm' option specifies javascript confirm box.
'Confirm' 选项指定 javascript 确认框。
回答by Nick Kahn
You can also customize the by passing the delete item along with the message. In my case using MVC and Razor, so I could do this:
您还可以通过将删除项与消息一起传递来自定义 。在我使用 MVC 和 Razor 的情况下,我可以这样做:
@Html.ActionLink("Delete",
"DeleteTag", new { id = t.IDTag },
new { onclick = "return confirm('Do you really want to delete the tag " + @t.Tag + "?')" })
回答by GirishBabuC
Try this :
尝试这个 :
<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>
回答by Hedego
Using webgrid you can found it here, the action links could look like the following.
使用 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()
)
回答by GirishBabuC
With image and confirmation on delete, which works on mozilla firefox
带有图像和删除确认,适用于 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>
回答by Antony D
I wanted the same thing; a delete button on my Details view. I eventually realised I needed to post from that view:
我想要同样的东西;我的详细信息视图上的删除按钮。我最终意识到我需要从那个角度发帖:
@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?');" />
}
And, in the Controller:
并且,在控制器中:
// 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
回答by Terry H
MVC5 with delete dialogue & glyphicon. May work previous versions.
@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()))
回答by Seeni Abirami
Any click event before for update /edit/delete records message box alerts the user and if "Ok" proceed for the action else "cancel" remain unchanged. For this code no need to right separate java script code. it works for me
更新/编辑/删除记录消息框之前的任何点击事件都会提醒用户,如果“确定”继续操作,否则“取消”保持不变。对于此代码,无需正确分离 java 脚本代码。这个对我有用
<a asp-action="Delete" asp-route-ID="@Item.ArtistID" onclick = "return confirm('Are you sure you wish to remove this Artist?');">Delete</a>
<a asp-action="Delete" asp-route-ID="@Item.ArtistID" onclick = "return confirm('Are you sure you wish to remove this Artist?');">Delete</a>