繁体   English   中英

如何通过使用ASP.NET MVC将复选框作为参数传递给控制器​​功能

[英]How to pass checkbox checked as a parameter in controller function using 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");

        }

最后一个示例:如果用户单击字母复选框,则布尔字母将保持为false而不是变为true。

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

更改所有复选框的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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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