7

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

1 Answer 1

24

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

Sign up to request clarification or add additional context in comments.

1 Comment

Is this good practice? set session values using ajax request?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.