Hen*_*nry 1 razor asp.net-mvc-4
在我的应用程序中,我需要将一些值从一个页面(页面 A 到页面 B)传递到另一页面。为此,我使用会话变量(我不能使用临时数据,因为它不适用于负载平衡)。在 Page AI 中设置会话变量。在Page BI中需要检索上面的Session变量。为此,我在页面 B 中使用隐藏字段。我不知道如何将会话变量设置为页面 B 中的隐藏字段。
[HttpPost]
public JsonResult GetFileName(string updatedfileName, string orgfileName)
{
Session["OrgFileName"] = orgfileName;
Session["UpdatedFileName"] = updatedfileName;
var result = myService.getFile(updatedfileName, orgfileName);
return Json(result, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
<div style="display:none" >
<input type="hidden" value="" id="hdnfilename" />
</div>
Run Code Online (Sandbox Code Playgroud)
在“Page B”的控制器中,将 a 设置ViewBag.MyValue为会话变量并将其应用于隐藏的值。
控制器
ViewBag.MyValue = Session["MYVALUE"];
Run Code Online (Sandbox Code Playgroud)
看法
<input type="hidden" value="@ViewBag.MyValue" id="hdnfilename" />
Run Code Online (Sandbox Code Playgroud)
如果您需要从 JavaScript 获取会话变量,则需要开发一个操作来返回会话变量并使用 JavaScript/jQuery 使用它,如下所示:
// Controller code
[HttpGet]
public JsonResult GetSessionVarValue(string key)
{
return Json(new { key = key, value = Session[key] }, JsonRequestBehavior.AllowGet);
}
// JavaScript code
var mySessionValue;
$.getJSON("/GetSessionVarValue", "MYKEY", function(data) {
mySessionValue = data.value;
});
Run Code Online (Sandbox Code Playgroud)
您也可以注意Session负载平衡中的变量。保护存储会话变量的最佳方法是将会话模式配置的状态更改为StateServer。http://msdn.microsoft.com/en-us/library/ms178586.aspx
| 归档时间: |
|
| 查看次数: |
26226 次 |
| 最近记录: |