简体   繁体   中英

How to pass checkbox checked as a parameter in controller function using ASP.NET MVC

I am creating a web program that generates a random password based on the parameters that the user chooses by checking/unchecking checkboxes. I am now unable to transfer the users choices to the controller as a parameter in one of the functions. I always get a false regardless of whether the user has checked the box or not, seeing as how I made use of optional parameters with the default value being false, just incase the user does not check all the boxes.

I have set the names of the checkboxes and i have used those names in the arguments of the function that I call. This however still gets me a false regardless of whether the user checked the boxes or not

This is the cshtml section of my code

 @using (Html.BeginForm("PasswordGenerator", "Generator", FormMethod.Post))
        {
                <table class="TablePre">

                    <tr class="TableTop">

                        <th>Preferences</th>

                    </tr>

                    <tr >
                        <td class="Spacing">
                            <input type="checkbox" value="letters" name="letters" id="iletters"/>
                            <label for="letters">Letters</label>
                        </td >
                    </tr>

                    <tr >
                        <td class="Spacing">
                            <input type="checkbox" value="numbers" name="numbers" id="inumbers" />
                            <label for="numbers">Numbers</label>
                        </td>
                    </tr>

                    <tr >
                        <td class="Spacing">
                            <input type="checkbox" value="symbols" name="symbols" id="isymbols" />
                            <label for="symbols">Symbols</label>
                        </td>
                    </tr>

                    <tr>
                        <td class="Spacing">
                            <input type="checkbox" value="words" name="words" id="iwords" />
                            <label for="words">Words</label>
                        </td>
                    </tr>

                    <tr>
                        <td class="Spacing">
                            <input type="checkbox" value="creative" name="creative" id="icreative" />
                            <label for="creative">Add my creative touch</label>
                        </td>
                    </tr>

                    <tr >
                        <td class="Spacing">
                            <label style="display: inline-block" for="pLength">Password Length:</label>
                            <br />
                            <input type="text"  name="PLChoice" id="pLength" />                    
                        </td>
                    </tr>

                <tr>

                    <td style="text-align:right;" class="TableTop">

                        <button type="submit"> Generate <span class="fas fa-cog"> </span> </button>

                    </td>

                </tr>

                </table> @* End of Table for password Generation preferences *@

                <br />
                <br />
                <br />

                <table class="TablePre TableSpacing">

                    <tr class="TableTop">

                        <th>Output</th>

                    </tr>


        } @* End of form *@

and this is the c# part of the code


        public ActionResult PasswordGenerator(bool letters = false , bool numbers = false, bool symbols = false, bool words = false, bool creative = false, int PLChoice = 0)
        {

            return View("Index");

        }

last example : If the user clicks on the letters checkbox, the Boolean letters will remain false instead of changing to true.

I expect the choices of the user to be transferred to the controller but what is happening is im getting false regardless of whether they checked the checkbox or not. This is what the web program looks like

Change the value="true" for all checkbox like

 <input type="checkbox" value="true" name="numbers" id="inumbers" />

Update tr as below

<tr>
            <td class="Spacing">
                <input type="checkbox" value="true" name="letters" id="iletters" />
                <label for="letters">Letters</label>
            </td>
        </tr>

        <tr>
            <td class="Spacing">
                <input type="checkbox" value="true" name="numbers" id="inumbers" />
                <label for="numbers">Numbers</label>
            </td>
        </tr>

        <tr>
            <td class="Spacing">
                <input type="checkbox" value="true" name="symbols" id="isymbols" />
                <label for="symbols">Symbols</label>
            </td>
        </tr>

        <tr>
            <td class="Spacing">
                <input type="checkbox" value="true" name="words" id="iwords" />
                <label for="words">Words</label>
            </td>
        </tr>

        <tr>
            <td class="Spacing">
                <input type="checkbox" value="true" name="creative" id="icreative" />
                <label for="creative">Add my creative touch</label>
            </td>
        </tr>

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