如何使用Knockout从vm设置会话变量?
I've seen a number of question on how to get a Session variable into knockout, but none explaining how to set one from a Knockout vm. My setup:
我已经看到了一些关于如何将一个Session变量转换为knockout的问题,但没有一个解释如何从Knockout vm中设置一个。我的设置:
-ASPX page where I get a Session["GridSize"] variable, and return it as a global variable called currentGridSize
-ASPX页面,我得到一个Session [“GridSize”]变量,并将其作为一个名为currentGridSize的全局变量返回
-VM where I get that global variable, and set this.gridSize = globals.gridSize
-VM我得到那个全局变量,并设置this.gridSize = globals.gridSize
-Dropdown that changes this.gridSize
-Dropdown更改this.gridSize
What I need:
我需要的:
-Some way to set Session["GridSize"] = this.gridSize, either when it changes or when the page is left
- 一些设置Session [“GridSize”] = this.gridSize的方法,无论是更改还是页面保留时
I have tried:
我努力了:
-Using a webmethod function on my .aspx.vb and calling that (Session variables cannot be called from a Shared function, and webmethods must be shared)
- 在我的.aspx.vb上使用webmethod函数并调用它(无法从共享函数调用会话变量,并且必须共享webmethods)
-Calling <%Session["CurrentPageIndex"]= self.currentPageIndex();%> from the vm
从vm中调用<%Session [“CurrentPageIndex”] = self.currentPageIndex();%>
1 个解决方案
#1
1
You can access Session
in an ASP.NET AJAX Page Method, by doing the following to your page method:
您可以通过对页面方法执行以下操作来访问ASP.NET AJAX页面方法中的Session:
<WebMethod(EnableSession := True)> _
Public Shared Sub StoreSessionValue(sessionValue As String)
' Set a value into Session
HttpContext.Current.Session("TheSessionValue") = sessionValue
End Sub
<WebMethod(EnableSession := True)> _
Public Shared Function GetSessionValue(sessionValueName As String) As String
' Get a value from Session
Return HttpContext.Current.Session(sessionValueName)
End Sub
Note: You must fully qualify the Session
object as HttpContext.Current.Session
.
注意:您必须将Session对象完全限定为HttpContext.Current.Session。
You can call this page method inside of your view model function, like this:
您可以在视图模型函数中调用此页面方法,如下所示:
$.ajax({
type: "POST",
url: "YourPage.aspx/GetSessionValue",
data: "{'sessionValueName':'sessionValue'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
// Do something with data returned here
}
});
更多相关文章
- 在页面加载时在shadowbox中加载视频
- js闭包与变量
- 有没有办法通过javascript检测关闭页面?
- JavaScript声明全局变量三种方式的异同
- JS在页面加载时候onload与匿名自调用函数的区别
- 父范围中的变量不会在匿名函数中被更改[重复]
- 传递变量以便以角度移动子弹
- 在变量名是字符串时声明变量?
- 如何将变量推送到web客户端以获取ajax?