【问题标题】:how to call javascript confirm dialog function in html.actionlink in asp.net mvc?如何在asp.net mvc的html.actionlink中调用javascript确认对话框函数?
【发布时间】:2014-11-25 00:47:29
【问题描述】:

当用户点击DELETE按钮时,我想显示一个确认对话框菜单。

但按钮不会触发我的脚本。触发 JavaScript 后,如果用户选择 OK,则脚本必须执行该操作。否则,如果用户单击取消,则不会发生任何事情。

这是 JavaScript 代码;

<script type="text/javascript">
    $(document).ready(function () {
        $(".confirmDialog").on("click", function (e) {
            // e.preventDefault(); use this or return false
            var url = $(this).attr('href');
            $("#dialog-confirm").dialog({
                autoOpen: false,
                resizable: false,
                height: 170,
                width: 350,
                show: { effect: 'drop', direction: "up" },
                modal: true,
                draggable: true,
                buttons: {
                    "OK": function () {
                        $(this).dialog("close");
                        window.location = url;
                    }, "Cancel": function () {
                        $(this).dialog("close");
                    }
                }
            });
            $("#dialog-confirm").dialog('open');
            return false;
        });
    });
</script>

这里是MVC代码;

<div style="text-align: right; font-weight: bold; font-size: larger; color: red;">
    <tr>
        <td>@Html.ActionLink("Delete", "Your-Action", new { @class = "confirmDialog" })</td>
    </tr>
</div>
<div id="dialog-confirm" style="display: none">
    <p>
        <span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
        Are you sure ?
    </p>
</div>

【问题讨论】:

  • 确保您的链接 html 看起来像您预期的那样

标签: javascript jquery asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

您正在使用的 Actionlink 重载需要第三个参数作为 objectroutes 而不是 htmlattributes。

正确的actionlink如图:

@Html.ActionLink("Delete", "Your-Action", new{} ,new { @class = "confirmDialog" })

或

@Html.ActionLink("Delete", "Your-Action", null ,new { @class = "confirmDialog" })

你的代码的其余部分都很好..!!

【讨论】:

  • @hislam...欢迎..如果答案解决了您的问题,请接受答案,以便接受的答案也能帮助其他人.....谢谢。
猜你喜欢
  • 2011-02-20
  • 1970-01-01
  • 1970-01-01
  • 2017-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多