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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Description
Just post to a controller and set the Session variable there.
Sample
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 });
}
}
More Information
- Save and retrieve Session data via Ajax using JQuery in an MVC 3 application