【问题标题】:How set Session variables in ASP.NET MVC 3 with jQuery?如何使用 jQuery 在 ASP.NET MVC 3 中设置会话变量?
【发布时间】:2012-01-03 20:26:13
【问题描述】:

所以问题来了:如何使用 jQuery 在 ASP.NET MVC 3 中设置会话变量?
我正在尝试使用$.ajax$.post,但问题是我真的不知道该怎么做。

【问题讨论】:

标签: jquery asp.net-mvc-3 session-variables


【解决方案1】:

说明

只需发布到控制器并在那里设置 Session 变量。

示例

jQuery

$(function () {
    $.post('/SetSession/SetVariable', 
           { key : "TestKey", value : 'Test' }, function (data) 
    {
        alert("Success " + data.success);
    });
});

Mvc 控制器

public class SetSessionController : Controller
{
    public ActionResult SetVariable(string key, string value)
    {
        Session[key] = value;

        return this.Json(new { success = true });
    }
}

更多信息

【讨论】:

  • 这是好的做法吗?使用 ajax 请求设置会话值?
猜你喜欢
  • 2016-05-10
  • 1970-01-01
  • 2015-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
  • 1970-01-01
相关资源
最近更新 更多