【问题标题】:How to use an action link to get a confirmation popup before going to a controller action?在进行控制器操作之前,如何使用操作链接获得确认弹出窗口?
【发布时间】:2014-09-08 10:58:19
【问题描述】:

我有一个调用控制器方法并传递一些数据以启用或禁用数据库系统中的用户的操作链接。

@Html.ActionLink("Change Status", "ChangeStatus", "Home", new { userName = ViewBag.userName, applicationName = item.Key, currentStatus = item.Value }, new { @class = "enableDisable" })

这个链接工作正常,除了在调用控制器方法之前应该出现一个 JQuery 弹出消息来确认操作。但是操作链接会绕过弹出窗口并直接调用控制器。

@section Scripts {

    <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
    <script src="http://code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
    <script type="text/javascript">

        $("#dialog-confirm").hide();
        $(function () {
            $(".enableDisable").click(function () {
                $("#dialog-confirm").dialog({
                    resizable: false,
                    height: 220,
                    width: 475,
                    modal: true,
                    buttons: {
                        "OK": function () {

                            $(this).dialog("close");

                            window.location.href = "~/Home/ChangeStatus/userName/applicationName/currentStatus/"
                        },
                        Cancel: function () {
                            $(this).dialog("close");
                        }
                    }
                });
            });
        });
    </script>
}

有人知道我的代码中缺少什么以使其正常工作吗?

编辑

JQuery 现在在一个 js 文件中,并被以下代码调用:

@section Scripts {
    <link href="~/Content/jquery-ui-1.11.1.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-ui-1.11.1.js"></script>
    <script src="~/Scripts/Dialog.js"></script>
}

JQuery 本身现在看起来像这样:

$("#dialog-confirm").hide();
$(function () {
    $(".enableDisable").click(function (e) {
        e.preventDefault()
        var url = $(this).attr('href');
        $("#dialog-confirm").dialog({
            resizable: false,
            height: 220,
            width: 475,
            modal: true,
            buttons: {
                "OK": function () {

                    $(this).dialog("close");
                    window.location.href = url;
                },
                Cancel: function () {
                    $(this).dialog("close");
                }
            }
        });
    });
});

剃刀视图:

@{
    ViewBag.Title = "User Details";
}

<h2>User Details</h2>

<p><b>@ViewBag.UserName</b></p>

    <table class="table">
         <tr>
             <th>
                 Application Name
             </th>
            <th>
               Status
            </th>
             <th>

             </th>
             <th>

             </th>

        </tr>

            @if (ViewBag.ApplicationStatuses.Count > 0)
            {
                @*Iterating Amadeus model using ViewBag *@
                foreach (var item in ViewBag.ApplicationStatuses)
                {
                <tr>
                    <td>
                        @item.Key
                    </td>
                    <td>
                        @item.Value
                    </td>
                    <td>
                       @Html.ActionLink("Change Status", "ChangeStatus", "User", new { userName = ViewBag.userName, applicationName = item.Key }, new { @class = "enableDisable" })
                    </td>
                    <td>
                        @Html.ActionLink("View Roles", "Roles", new { userName = ViewBag.UserName, applicationName = item.Key })
                    </td>
                </tr>
                }
            } 

</table>
<div id="dialog-confirm" title="Change Status?">
    Are you sure you want to change the status of: @ViewBag.UserName?
</div>

@section Scripts {

    <link href="~/Content/jquery-ui-1.11.1.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-ui-1.11.1.js"></script>
    <script src="~/Scripts/Dialog.js"></script>
}

【问题讨论】:

    标签: c# javascript jquery asp.net-mvc-5 actionlink


    【解决方案1】:

    尝试使用e.preventDefault() 并进行一些更改,如下所示:-

    <script type="text/javascript">
    
        //$("#dialog-confirm").hide();
        $(function () {
            $(".enableDisable").click(function (e) {
                e.preventDefault();
                var url = $(this).attr('href');
                $("#dialog-confirm").dialog({
                    resizable: false,
                    height: 220,
                    width: 475,
                    modal: true,
                    buttons: {
                        "OK": function () {
    
                            //$(this).dialog("close");
    
                            window.location.href = url;  //<-------
                        },
                        "Cancel": function () {
                            $(this).dialog("close");
                        }
                    }
                });
              $("#dialog-confirm").dialog("open"); // <------- open dialog this way.
            });
        });
    </script>
    

    【讨论】:

    • 不幸的是这没有帮助,我已经更新了我的问题以提供 js 文件中的代码和调用 js 文件的代码。
    • @SlipperyBalmain ...对不起,但我不希望您现在尝试做...并在我的回答中仔细查看您没有在...函数中使用'e'(e )...??
    • 嗯实际上这是一个好点,我现在有那个代码并且 jquery 不起作用。此刻我似乎正在做出奇怪的改变。
    • 我正在尝试做同样的事情,但不是我的 jQuery 与操作链接在同一个文件中,而是现在在一个 js 文件中,并且在包含操作链接的文件中有一个脚本引用.我现在已经包含了整个剃须刀代码以供查看。
    • 我找不到。问题似乎是,当我尝试更改任何应用程序的状态时,仅更新列表中的第一个应用程序。只有当我的弹出窗口被激活并且 javascript 位于剃刀视图的单独文件中时,才会出现问题。通过调试,我发现 jquery 弹出按钮只找到创建的第一个操作链接的 href,但我似乎无法弄清楚为什么它在同一个文件中工作时。这是否更好地解释了这个问题?我可以采取哪些步骤来尝试解决此问题?
    猜你喜欢
    • 1970-01-01
    • 2021-03-28
    • 2014-03-26
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多