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