检测页面在asp.net MVC3关闭(或卸载) [英] detecting page close (or unload) in asp.net mvc3

查看:363
本文介绍了检测页面在asp.net MVC3关闭(或卸载)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个MVC3应用程序和几页,我需要当用户导航离开该页面进行一些操作。

I am building a MVC3 app and on few pages I need to perform few action when the user navigates away from the page.

例如,如果用户在页面X和点击其他任何链接,我需要执行的操作A,B,C

For example if the user is on page X and clicks any other link I need to perform actions A,B,C

实施我迄今使用过滤器,如果用户浏览到从第X页

The implementation I have so far uses filters and detects if the user navigates to any other page from page X.

这工作,除非用户在花药标签打开页X的链接。即在页X仍然是开放的,但用户导航到另一个页面和操作A,B,C和执行。这不是假设发生,因为页X仍然是开放的。

This works except if the user open the link on page X in anther tab. i.e the page X is still open but the user has navigated to another page and actions A, B, C and performed. This not suppose to happen since page X is still open.

我想知道是否有办法来检测接近甚至在页X页,所以我只在页面被关闭或卸载执行的操作?

I was wondering if there is way to detect page close even on page X so I only perform the action when the page is closed or unloaded ?

推荐答案

您可以使用jQuery的 .unload 与阿贾克斯后配对功能,从前端做到这一点。

You can use jQuery's .unload functionality paired with an ajax post to accomplish this from the front end.

$(window).unload(function() {
 //ajax post to controller action
 $.ajax({
        type: 'POST',
        url: '/Controller/Action',
        data : { param1 : "value1", param2: "value2" },
        dataType: 'html',
        success: //do what needs to be done here, alert?
    });
});

这篇关于检测页面在asp.net MVC3关闭(或卸载)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆