I am using MVC3 with Razor View engine,
i have .cshtml page in that i have a JavaScript function, inside that JavaScript function, i want to create Session variable and retrieve that session in same JavaScript function.
how to achieve this..
The Session is on the server side so you need to call the server in order to set or retrieve session variables.
Just post to a controller and set the Session variable there.
jQuery
$(function () {
$.post('/SetSession/SetVariable',
{ key : "TestKey", value : 'Test' }, function (data)
{
alert("Success " + data.success);
});
});
Mvc Controller
public class SetSessionController : Controller
{
public ActionResult SetVariable(string key, string value)
{
Session[key] = value;
return this.Json(new { success = true });
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With