如果在具有可选标签的 Html.Dropdownlist() 帮助器中未选择任何内容,则 Html.BeginForm() 不会提交

发布于 2024-11-30 02:45:58 字数 1546 浏览 7 评论 0原文

@using (Html.BeginForm("Boxing", "Company",FormMethod.Post))
{ 
    <div class="box">
        <div>
            <div class="left">
                <div class="topLabel">
                    Id No:</div>
                <div class="input_text_65">
                    @Html.TextBoxFor(m => m.Id)
                </div>
                <div class="botLabel">
                    percentt:
                </div>
                <div>
                    <input type="text" style="width: 50px" name="percent" />%
                </div>
            </div>
            <div class="lastDetailField">
                <div class="topLabel">
                    D/C:
                </div>
                <div class="select_110">
                    @Html.DropDownListFor(m => m.cType, new SelectList((IEnumerable<Model.cType>)ViewData[ViewDataKeys.cTypes]), "----")
                </div>
                <div class="margin_top_45">
                    <input id="submit" type="submit" value="submit" />
                </div>
            </div>
        </div>
    </div>
}

选项,则不会发布表单,并且焦点将移至下拉列表

如果我没有在下拉列表中选择任何选项(保留选项标签“----”处于选中状态)并且按下提交按钮,则如果我删除可选 标签,像这样:

@Html.DropDownListFor(m => m.cType, new SelectList((IEnumerable)ViewData[ViewDataKeys.cTypes]))

那么它就可以正常工作了。我在想如果我贴上可选标签,是否意味着我需要选择一个项目?即使我在下拉列表中选择了可选标签,我也希望提交表单。

提前致谢!

@using (Html.BeginForm("Boxing", "Company",FormMethod.Post))
{ 
    <div class="box">
        <div>
            <div class="left">
                <div class="topLabel">
                    Id No:</div>
                <div class="input_text_65">
                    @Html.TextBoxFor(m => m.Id)
                </div>
                <div class="botLabel">
                    percentt:
                </div>
                <div>
                    <input type="text" style="width: 50px" name="percent" />%
                </div>
            </div>
            <div class="lastDetailField">
                <div class="topLabel">
                    D/C:
                </div>
                <div class="select_110">
                    @Html.DropDownListFor(m => m.cType, new SelectList((IEnumerable<Model.cType>)ViewData[ViewDataKeys.cTypes]), "----")
                </div>
                <div class="margin_top_45">
                    <input id="submit" type="submit" value="submit" />
                </div>
            </div>
        </div>
    </div>
}

If I didn't select any option in the dropdownlist (leaving the optionlabel "----" selected) and I press the submit button, the form will not be posted and the focus will be move to the dropdownlist

if I remove the optional Label, like this:

@Html.DropDownListFor(m => m.cType, new SelectList((IEnumerable)ViewData[ViewDataKeys.cTypes]))

then it will work just fine. I'm thinking if I will put an optional label, does that mean I am required to choose an item? I want the form to be submitted even if I leave the dropdownlist with the optionallabel selected.

thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别忘他 2024-12-07 02:45:58

您的 cType 模型/视图模式中是否有必需的属性?我认为客户端验证正在启动,但由于您没有设置验证助手,因此您看不到该消息。如果您选择可选标签,则不会提交表单。

Do you have a required attribute in your model/viewmoden on cType? I think the clientside validation is kicking in, but because you didn't set a validation helper, you're not seeing the message. The form won't submit though if you select the optional label.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文