How can I access html elements in the IsPost section of the razor code? RRS feed

  • Question

  • User-1484187403 posted

    I want to be able to save the checked state of several input checkboxes in cookies, so that I can read those 

    cookies to alter the state of a page (disable/invisiblize whichever tab pages the user deselected by unchecking
    the corresponding check box).

    I can *set* the checkboxes to either checked or unchecked based on boolean variables values, and I can *read* the cookies easily enough, but setting/writing the *cookie values* has been beyond me so far. I have Razor code like this:

    if (IsPost) {
    Response.Cookies["someCookieVal"] = // how do I access the checked state of the checkbox here?

    If this is not possible, or requires the sacrificing of chickens to accomplish, what is another way to save this cat's skin?

    Wednesday, May 29, 2013 5:33 PM


  • User-821857111 posted

    A checkbox appears in the Request.Form collection only if it is checked. Otherwise it won't exist. So you can do this:

        Response.Cookies["someCookieVal"].Value = Request["check"].IsEmpty() ? "not checked" : "checked";

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 30, 2013 12:52 AM