Reputation: 883
In my application I need to pass some values from one page(page A to Page B) to another page. For this I am using Session variables(I cannot use Tempdata as it doesn't work on loadbalancing). In Page A I am setting the Session Variable. In Page B I need to retrieve the above Session variable. For this I am using a Hidden field in Page B. I dont know how to set the Session Variable to Hidden Field in Page 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);
}
<div style="display:none" >
<input type="hidden" value="" id="hdnfilename" />
</div>
Upvotes: 1
Views: 26359
Reputation: 1643
In the controller of "Page B", set a ViewBag.MyValue
to your session variable and apply it to the hidden's value.
Controller
ViewBag.MyValue = Session["MYVALUE"];
View
<input type="hidden" value="@ViewBag.MyValue" id="hdnfilename" />
If you need to get a session variable from JavaScript, you will need to develop an action that will return the session variable and consume it with JavaScript/jQuery, like this:
// 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;
});
You may take care with Session
variables in load balance, too. The best way to secure store session variables is changing the state of session mode configuration to StateServer
. http://msdn.microsoft.com/en-us/library/ms178586.aspx
Upvotes: 5