如何使用 jQuery 在 ASP.NET MVC 3 中设置会话变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8711998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How set Session variables in ASP.NET MVC 3 with jQuery?
提问by Arсiom Prudnika?
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.
所以这就是问题:如何使用 jQuery 在 ASP.NET MVC 3 中设置会话变量?
我正在尝试使用$.ajax
or$.post
但问题是我真的不知道该怎么做。
回答by dknaack
Description
描述
Just post to a controller and set the Session variable there.
只需发布到控制器并在那里设置 Session 变量。
Sample
样本
jQuery
jQuery
$(function () {
$.post('/SetSession/SetVariable',
{ key : "TestKey", value : 'Test' }, function (data)
{
alert("Success " + data.success);
});
});
Mvc Controller
MVC控制器
public class SetSessionController : Controller
{
public ActionResult SetVariable(string key, string value)
{
Session[key] = value;
return this.Json(new { success = true });
}
}