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

后端 未结 1 1447
青春惊慌失措
青春惊慌失措 2021-02-09 06:59

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\

1条回答
  •  滥情空心
    2021-02-09 07:15

    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

    0 讨论(0)
提交回复
热议问题