How can I post a checkbox value to a controller that saves value to session?

I have the following code on checkbox click to get the checkbox value of the checkbox showHideDeletedRps and pass true or false to a controller var rpInfo = await rpService.GetRPByID(id, showDeleted);, I tried the following but it doesn’t keep the selected value or doesn’t update on checkbox change.

How can I apply the same logic using session?

@{
    var showDeleted = true; 
    if (Context.Request.HasFormContentType && Context.Request.Form.TryGetValue("showHideDeletedRps", out var formValue)
        && bool.TryParse(formValue, out var parsedFormValue))
    {
        showDeleted = parsedFormValue;
    }
    else if (Context.Request.Query.TryGetValue("showHideDeletedRps", out var queryValue)
        && bool.TryParse(queryValue, out var parsedQueryValue))
    {
        showDeleted = parsedQueryValue;
    }
 var id = Model.ID;
 var rpInfo = await rpService.GetRPByID(id, showDeleted);// used dependency injection to populate fields in a View Component 
}

I used this javascript code to

<script type="text/javascript">
    $("select[autopostback=true],input[type=checkbox][autopostback=true],input[type=radio][autopostback=true]").on("change", function () {
        $(this).closest("form").submit();
    });
</script>

On-change checkbox

<input autopostback="true" name="showHideDeletedRps" id="showHideDeletedRps" type="checkbox" value="true">

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you just want to pass the value of the checkbox to a certain action of the controller when the checkbox is clicked, you can directly achieve it by the form to complete:

@{
    ViewData["Title"] = "Test";
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
<form asp-action="test" method="post">
    <input autopostback="true" name="showHideDeletedRps" id="showHideDeletedRps" type="checkbox" value="true">
</form>
@section Scripts{
    <script type="text/javascript">
        if ('@ViewData["isChecked"]' == "True") {
            $('#showHideDeletedRps').prop('checked', true);
        } else {
            $('#showHideDeletedRps').prop('checked', false);
        }
        $("select[autopostback=true],input[type=checkbox][autopostback=true],input[type=radio][autopostback=true]").on("change", function () {
            $(this).closest("form").submit();
        });
    </script>
}

Controller:

    [HttpPost]
    public IActionResult Test(bool showHideDeletedRps)
    {
        ViewData["isChecked"] = showHideDeletedRps;
        return View();
    }

Here is the test result:

How can I post a checkbox value to a controller that saves value to session?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x