前台代码:
function RunOnBeforeUnload() {
//更改用户登录状态
var currUser = getCOOKIE("UserAccount");
$.ajax({
type: "post",
url: "../Home/UpdateLoginState",
data: { 'currUser': currUser},
dataType: "text",
async: false,
success: function (data) {
}
});
}
后台代码:
//关闭浏览器后更改用户登录状态
public ActionResult UpdateLoginState()
{
string currUser = "";
string stateStr = "";
if (Request["currUser"]!=null)
{
currUser = Request["currUser"].ToString();
string updaStr = "update Users set OnLine=0 where UserAccount='"+currUser.Trim()+"'";
int coun = SqlHelper.ExecuteSql(updaStr);
if (coun > 0)
{
stateStr = "更改成功!";
}
else
{
stateStr = "更改失败!";
}
}
return Content(stateStr);
}