Asp.net-Mvc-3

帶有確認對話框的 MVC3 Actionlink

  • February 15, 2021

我可以顯示一個確認消息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)函式向使用者顯示帶有指定消息的確認對話框,並根據使用者的響應返回真或假。

引用自:https://stackoverflow.com/questions/10198226