我可以显示一个确认消息ActionLink
吗?
我需要使用javascript吗?没有它有可能吗?
你能给我举个例子吗?
谢谢你。
//I want to make a confirmation message appear before the link opens.
@Html.ActionLink("Checkout and view order list", "Order", "Order")
我可以显示一个确认消息ActionLink
吗?
我需要使用javascript吗?没有它有可能吗?
你能给我举个例子吗?
谢谢你。
//I want to make a confirmation message appear before the link opens.
@Html.ActionLink("Checkout and view order list", "Order", "Order")
使用重载Html.ActionLink(string linkText, string actionName, string controllerName, object RouteValues, object HtmlAttributes)
和一些 javascript,您可以执行以下操作:
@Html.ActionLink("Checkout and view order list", "Order", "Order", null, new { onclick="return confirm('Are you sure you want to click this link?')" })
这将添加 HTML 属性 onclick,该属性将在单击链接时执行指定的 javascript。如果链接(或表单的提交按钮)上的 onclick 事件返回 false,则操作(跟随链接,发布表单)不会发生。该confirm(message)
函数向用户显示带有指定消息的确认对话框,并根据用户的响应返回真或假。
编辑:不要使用这个答案,使用 Jim 的另一个答案。
您将无法使用ActionLink
- 您必须编写一些 JavaScript(例如此处列出的)才能弹出确认。您可以使用Url.Action
生成 javascript 最终将用于 post 或 get 端点的 URL。
我的 javascript 很糟糕,但我认为这可以理解:
<a href="javascript:confirmation();">Checkout and view order list</a>
<script>
function confirmation() {
var answer = confirm("Confirm?")
if(answer) {
// Do something here, post or get
window.location = @Url.Action("Order", "Order");
}
}
</script>
就我而言,我有一个已经调用动作的按钮:
<input id="ButtonDelete" type="button" value="Delete Experiment" class="big-nevigation-button" onclick="location.href='@Url.Action("DeleteExperiment", "Experiment", new { experimentId = Model.ExperimentId })'" />
阅读吉姆的答案后,我将其更改为以下内容:
<input id="ButtonDelete" type="button" value="Delete Experiment" class="big-nevigation-button" onclick="if (confirm('Are you sure you want to delete experiment???')) location.href='@Url.Action("DeleteExperiment", "Experiment", new { experimentId = Model.ExperimentId })'" />
而且效果更好!谢谢吉姆!!
在 MVC Core 中,我使用了这个,但仍然在 JavaScript 中
<button type="submit"
asp-action="Delete" asp-route-id="@Model.Id"
onclick="return confirm('Are you sure you want to delete?');">
</button>