Siddhesh Adarkar
Siddhesh Adarkar

Reputation: 31

Clear Session on browser close in MVC

I am developing as MVC 5 application. I want store the users logout time in database. In my web page i have different Links (@Html.ActionLink). In my controller i have written code to store logout time. The same is working. However, the problem is Action method is called even when Link is clicked. So I added flag to check whether link is clicked or not ... No luck ... now logout method is not at all call.

Below is my Java script

$(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) {

                    }

                })
            };

        });

Upvotes: 0

Views: 1274

Answers (2)

Mohamed Allam
Mohamed Allam

Reputation: 69

why don't you call the code directly

$(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) {

            }

        });
    });

});

Upvotes: 1

rashfmnb
rashfmnb

Reputation: 10588

use html

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

Upvotes: 0

Related Questions