How to set session variables in ASP.NET MVC 3 using jQuery?

So here is the question: how to set session variables in ASP.NET MVC 3 using jQuery?
I am trying to use $.ajax or $.post , but the problem is that I really don't know what to do.

+7
source share
1 answer

Description

Just send a message to the controller and set the session variable there.

Example

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

Additional Information

+20
source

All Articles