Click here to Skip to main content
14,929,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If user closed the web browser then automatically session history is erased, but I have to update it in my database whether user still in the session or not

1 solution

In your layout you should use Javascript like in the next example;

<script type="text/javascript">

        function onWindowClosing() {
            if (window.event.clientX < 0 || window.event.clientY < 0) {
                    type: "POST",
                    url: "/Account/OnWindowClosing"

        function onKeydown(evt) {
            if (evt != undefined && evt.altKey && evt.keyCode == 115) //Alt + F4 
                    type: "POST",
                    url: "/Account/OnWindowClosing"

        window.onbeforeunload = onWindowClosing;
        window.document.onkeydown = onKeydown; 

Then in your Account controller you have to implement the method OnWindowClosing to save what you want.

In my next article: MVC Basic Site: Step 4 – jqGrid Integration in MVC 4.0 using AJAX, JSON, jQuery, LINQ, and Serialization[^] I have provided also source code, and you can see the complete implementation of the above events. Have a look especially on _Layout.cshtml layout page and on AccountController class.
kishore@cse 23-Mar-14 4:31am
It's working OK, but I want more
Raul Iloc 23-Mar-14 9:12am
So you could accept my solution!
Raul Iloc 24-Mar-14 3:26am
See my update with source code link provided.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900