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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM