·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> ASP.NET MVC下实现前端视图页的Session

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

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

asp.net MVC下实现前端视图页的session

在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>
}