您的位置:首页 > 编程语言 > ASP

ASP.NET MVC下实现前端视图页的Session

2015-04-22 22:52 337 查看
在ASP.NET MVC的控制器中可以实现Session处理。如果要在前端视图页实现Session该如何做呢?可以使用window.sessionStorage来做。 AlexChittock用jQuery做了实现。在这里: https://github.com/AlexChittock/JQuery-Session-Plugin

具体实现很简单:

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<input type="text" id="guess"/>
<br/>
<input type="button" id="btn" value="我猜"/>
@section scripts
{
<script src="~/Scripts/jquery.session.js"></script>
<script type="text/javascript">
        $(function() {
//$.session.set('some key', 'a value');
//$.session.get('some key');
//$.session.clear();
//$.session.remove('some key');
$.session.set(mySessionKey, "Hello World");
            $('#btn').on("click", function() {
                if ($('#guess').val() == $.session.get(mySessionKey)) {
alert("恭喜你猜对了~~");
                }
});
});
var mySessionKey = "mykey";
</script>
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: