在MVC中关闭浏览器的清除会话

时间:2016-03-12 17:22:15

标签: jquery asp.net-mvc-5

我正在开发MVC 5应用程序。我希望将用户注销时间存储在数据库中。在我的网页中,我有不同的链接( @ Html.ActionLink )。在我的控制器中,我编写了用于存储注销时间的代码。同样的工作。但是,问题是即使单击链接也会调用Action方法。所以我添加了标志来检查链接是否被点击......没有运气......现在注销方法根本就没有了。

以下是我的Java脚本

$(document).ready(function () {
            var isCliked;
$(".link").click(function () {

            isCliked = "Y";
        });
$(window).bind("beforeunload", function () {

            if (!isCliked == "Y") {
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: '@Url.Action("ClearSession", "Login")',

                    data: {},
                    success: function (d) {
                        alert("About to Close Session");
                        window.location.reload();


                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {

                    }

                })
            };

        });

2 个答案:

答案 0 :(得分:1)

为什么不直接调用代码

$(document).ready(function () {

    $(".link").click(function () {
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: '@Url.Action("ClearSession", "Login")',

            data: {},
            success: function(d) {
                alert("About to Close Session");
                window.location.reload();
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {

            }

        });
    });

});

答案 1 :(得分:0)

使用html

<a href="#" onclick="call function to save logout time"> LogOut</a>
相关问题