简体   繁体   English

如何在asp.net mvc中的html.actionlink中调用javascript确认对话框功能?

[英]how to call javascript confirm dialog function in html.actionlink in asp.net mvc?

I want to show a confirm dialog menu, when user clicks DELETE button. 当用户单击DELETE按钮时,我想显示一个确认对话框菜单。

But button doesn't trigger my script. 但是按钮不会触发我的脚本。 After triggering JavaScript, if user chose OK, script must go to the action. 触发JavaScript后,如果用户选择“确定”,则脚本必须转到该动作。 Else, if user clicks the CANCEL, nothing should happen. 否则,如果用户单击“取消”,则什么也不会发生。

Here is the JavaScript code; 这是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>

Here is MVC codes; 这是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>

The Overload of Actionlink which you are using expects 3rd parameter as objectroutes not htmlattributes . 您正在使用的Actionlink重载期望第3个参数作为对象路由而不是htmlattributes

Correct actionlink as shown: 更正ActionLink的 ,如下所示:

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

OR 要么

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

The rest of your code is just fine..!! 您的其余代码都很好.. !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM