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