首页 > 解决方案 > 如何使用 ASP.NET MVC 在控制器函数中将选中的复选框作为参数传递

问题描述

我正在创建一个 Web 程序,该程序根据用户通过选中/取消选中复选框选择的参数生成随机密码。我现在无法将用户选择作为其中一项功能的参数传输到控制器。无论用户是否选中该框,我总是得到一个错误,看看我如何使用默认值为 false 的可选参数,以防用户没有选中所有框。

我已经设置了复选框的名称,并且在我调用的函数的参数中使用了这些名称。然而,无论用户是否选中了这些框,这仍然让我误会

这是我的代码的 cshtml 部分

 @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 *@

这是代码的c#部分


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

            return View("Index");

        }

最后一个例子:如果用户点击字母复选框,布尔字母将保持为假而不是变为真。

我希望将用户的选择转移到控制器,但是无论他们是否选中了复选框,发生的事情都是错误的。这就是网络程序的样子

标签: c#asp.net-mvcrazor

解决方案


更改value="true"所有复选框,例如

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

更新 tr 如下

<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>

推荐阅读