How set Session variables in ASP.NET MVC 3 with jQuery?

梦想的初衷 提交于 2019-12-09 05:54:53

问题


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


来源:https://stackoverflow.com/questions/8711998/how-set-session-variables-in-asp-net-mvc-3-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!