如何使用挖空从虚拟机设置会话变量

How do you set a session variable from the vm with Knockout?

本文关键字:设置 会话 变量 虚拟机 何使用      更新时间:2023-09-26

我已经看到许多关于如何让会话变量进入淘汰赛的问题,但没有一个解释如何从淘汰赛虚拟机中设置一个。 我的设置:

-ASPX 页面,我在其中获取一个 Session["GridSize"] 变量,并将其作为名为 currentGridSize 的全局变量返回

-VM,我在其中获取该全局变量,并设置此.gridSize = globals.gridSize

-更改此.gridSize的下拉列表

我需要什么:

- 某种设置会话["网格大小"] = this.gridSize 的方法,当它更改或页面离开时

我试过:

-在我的.aspx.vb上使用网络方法函数并调用它(会话变量不能从共享函数调用,并且必须共享网络方法)

-从 vm 调用 <%Session["CurrentPageIndex"]= self.currentPageIndex();%>

您可以通过对页面方法执行以下操作来访问 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

注: 必须将Session对象完全限定为 HttpContext.Current.Session

您可以在视图模型函数中调用此页面方法,如下所示:

$.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
  }
});