【问题标题】:MVC3 Actionlink with confirmation dialog带有确认对话框的 MVC3 Actionlink
【发布时间】:2012-04-29 04:56:00
【问题描述】:

我可以显示ActionLink 的确认消息吗?

我需要使用 javascript 吗?没有它可以吗?

你能给我举个例子吗?

谢谢。

//I want to make a confirmation message appear before the link opens.
@Html.ActionLink("Checkout and view order list", "Order", "Order")

【问题讨论】:

    标签: asp.net-mvc-3 confirmation


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      在我的例子中,我有一个按钮已经调用了一个动作:

      <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 })'" />
      

      而且效果更好! 谢谢吉姆!!

      【讨论】:

        【解决方案3】:

        编辑:不要使用这个答案,使用 Jim 的另一个答案。

        您将无法使用 ActionLink - 您必须编写一些 JavaScript(例如列出的 here)才能弹出确认信息。您可以使用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>
        

        【讨论】:

        • ActionLink 仍然可以使用。它只是无法自行确认。
        • 哇,不知道。很棒的解决方案,+1
        • 是的,ActionLink 有大量的重载(有时尝试使用正确的重载会变得混乱)。由于 RouteValues 和 HtmlAttributes 也非常灵活,你可以用 ActionLink 做很多事情
        【解决方案4】:

        使用重载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) 函数向用户显示带有指定消息的确认对话框,并根据用户的响应返回 true 或 false。

        【讨论】:

          猜你喜欢
          • 2011-06-08
          • 2015-06-26
          • 1970-01-01
          • 2012-03-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多