简体   繁体   中英

Using session to store state of a checkbox

I'm making an MVC web app. I need a checkbox on my main index page which if checked will update certain options on other pages. I'm thinking I could save the state of this checkbox into the session so that it can be accessed later.

Is this the correct direction to head in?

Also I believe session can't be called with javascript, so would I handle this with an AJAX call?

Thanks for any help in the right direction.

EDIT:

This app shows several possible tables to the user depending on what they select. These can then be updated on separate pages. This checkbox will filter out some of the options that could show up on these other pages. Using a model won't work (I think) because I have other models being sent to these pages instead. I suppose I could add this variable to each of those models and update it, but that seems like a hassle without any real gain unless I am missing something.

EDIT 2:

This is what I have so far. It seems to be working.

$(function () {
    $("#checkBox1").change(function () {
        console.dir($("#checkBox1"));
        var choice = $("#checkBox1")[0].checked;
        console.log(choice);
        $.ajax({
            cache: false,
            type: "POST",
            url: RootDirectory + "Home/SetCheckBoxSession/",
            data: { "choice": choice },
            success: function () {
                alert("Updated session with " + choice);
            },
            error: function (xhr, ajaxOptions, throwError) {
                alert("Error changing session");
            }
        });

    })
})

[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
public void SetCheckBoxSession(bool choice)
{
    Session["IsCheckBoxSet"] = choice;
}

Any thoughts?

This would be an acceptable use of the session, though if you want any sort of real persistence to this setting, you should add it as a property on your user entity or use a claim. With session, you must assume the session could be destroyed at any time, so nothing vital to the application should be stored there.

While true that you can't directly access the session via JavaScript, you can use an AJAX call to hit an action that will retrieve the value from the session and send it back as a response. However, there's not really a strong need to do so unless you're expecting this value to be changed between requests. Otherwise, just use the Session object directly in your controller action or view to determine what the rendered output will be. Or, if you actually need to know the value in your JavaScript, you can either set a JavaScript variable on page (preferably within a namespace for your application) or add a hidden field or something containing the value that you can retrieve from in your JavaScript.

Note: If you go the AJAX route, use an action in a child of the MVC Controller , not Web API's ApiController . The latter has no access to Session because Web API is REST-compliant and REST is stateless.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM