So this is the question: how set Session variables in ASP.NET MVC 3 with jQuery?
I'm trying to use $.ajax
or $.post
but the problem is that I don't really know what to do.
You cannot "Access Session from jQuery". You use MVC and asp.net to create an HTML page (with JavaScript). Session is a server-side object, and JavaScript runs on the client side.
ToInt32(val); Session["AshDiffuserID"] = ash_diffuser_id; } else if(key == "PesterchumHandle") { string handle = val; Session["PesterchumHandle"] = handle; } else // etc... After that, you'll need to set up a post HTTP request through jquery that puts whatever values you need in those "key" and "val" fields.
jQuery is a JavaScript library and JavaScript is a Client Side language and hence directly it is not possible to set Session variable in jQuery.
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