javascript 正在验证 MVC 隐藏字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15313420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:20:36  来源:igfitidea点击:

MVC hidden field being validated

javascriptc#jqueryasp.netasp.net-mvc

提问by Sachin Kainth

I have a few fields on my page that appear and dissappear depending on the dropdown selections you make on the page.

我的页面上有几个字段会出现和消失,具体取决于您在页面上所做的下拉选择。

So, for example, I have

所以,例如,我有

<section>
            @Html.LabelFor(model => model.AuctionTypeId)
            <div> @Html.DropDownList("AuctionTypeId", Model.AuctionTypes, @AuctionControllerResource.SelectAuctionType, new { id="auctionType", required = "required" })
                @Html.ValidationMessageFor(model => model.AuctionTypeId)       </div>
        </section>
        <section>
            @Html.LabelFor(model => model.AutomaticExtensionType, new { hidden = "true", id = "automaticExtensionTypeLabel" })
            <div> @Html.DropDownList("AutomaticExtensionType", Model.AutomaticExtensions, @AuctionControllerResource.SelectAutomaticExtensionType, new { hidden="hidden", required = "required", id = "automaticExtensionTypeList" }) 
                @Html.ValidationMessageFor(model => model.AutomaticExtensionType)       </div>
        </section>

The JQuery code I have for this is

我为此拥有的 JQuery 代码是

$('#auctionType').change(function () {
    var selectedAuctionType = $("#auctionType").val();
    var englishAuctionType = $("#englishAuctionTypeId").val();
    if (selectedAuctionType == englishAuctionType) {
        $("#automaticExtensionTypeList").show();
        $("#automaticExtensionTypeLabel").show();
    } else {
        $("#automaticExtensionTypeList").hide();
        $("#automaticExtensionTypeLabel").hide();
    }
});

Now, the showing and hiding work like they should. The problem is that when I submit the form and the field automaticExtensionTypeListis hidden, the form doesn't submit because automaticExtensionTypeListis a required field. The question is how can I tell MVC to only validate visible fields?

现在,显示和隐藏工作正常。问题是,当我提交表单并且该字段automaticExtensionTypeList被隐藏时,该表单不会提交,因为automaticExtensionTypeList它是必填字段。问题是如何告诉 MVC 只验证可见字段?

I've had a look through some of the JQuery we have written in this project and we have this line

我已经浏览了我们在这个项目中编写的一些 JQuery,我们有这一行

$.validator.setDefaults({ ignore: [] });

Apparantly this enables hidden validation. My question is, what line of code does the opposite?

显然,这会启用隐藏验证。我的问题是,哪行代码做相反的事情?

回答by Dave Alperovich

Try this:

试试这个:

$.validator.setDefaults({
    ignore: ':hidden, [readonly=readonly]'
});

or for customizes

或定制

$.validator.setDefaults({
    ignore: "#automaticExtensionTypeList" 
});

回答by Ant P

The reason for this is that hidden fields are not designed for use when a field doesn't apply - they're designed for use when a field doesapply but doesn't require input from the user. In your situation, you could:

这样做的原因是,隐藏字段不是设计时使用一个字段不适-他们设计用于在现场适用,但不要求用户输入。在您的情况下,您可以:

  • Use view logic to avoid rendering the field at all unless it is applicable.
  • Use a custom validatorinstead of just a flat-out Required.
  • Use a valid 'default' value in the hidden field.
  • 使用视图逻辑来避免渲染字段,除非它适用。
  • 使用自定义验证器而不是简单的Required.
  • 在隐藏字段中使用有效的“默认”值。

Personally, I'd go for the last as it'll be quickest to implement and doesn't have any obvious pitfalls - you can pick up this default value in your controller and then manipulate it as needed.

就我个人而言,我会选择最后一个,因为它实现起来最快,并且没有任何明显的缺陷 - 您可以在控制器中选择这个默认值,然后根据需要对其进行操作。

回答by Renaissance

This is a common problem.

这是一个常见的问题。

Hiddens will not stop validation.

隐藏不会停止验证。

I always handle this by creating a multiple forms with overlapping properties.

我总是通过创建具有重叠属性的多个表单来处理这个问题。

example:

例子:

<div id="HasControlls">
@Using(Html.BeginForm)
{
            @Html.LabelFor(model => model.AuctionTypeId)
            @Html.DropDownList("AuctionTypeId", Model.AuctionTypes, @AuctionControllerResource.SelectAuctionType, new { id="auctionType", required = "required" })
                @Html.ValidationMessageFor(model => model.AuctionTypeId)       </div>
        </section>
        <section>
            @Html.LabelFor(model => model.AutomaticExtensionType, new { hidden = "true", id = "automaticExtensionTypeLabel" })
            <div> @Html.DropDownList("AutomaticExtensionType", Model.AutomaticExtensions, @AuctionControllerResource.SelectAutomaticExtensionType, new { hidden="hidden", required = "required", id = "automaticExtensionTypeList" }) 
                @Html.ValidationMessageFor(model => model.AutomaticExtensionType)       </div>
}



<div id="HasNotControlls">
@Using(Html.BeginForm)
{
            @Html.LabelFor(model => model.AuctionTypeId)
            @Html.DropDownList("AuctionTypeId", Model.AuctionTypes, @AuctionControllerResource.SelectAuctionType, new { id="auctionType", required = "required" })
                @Html.ValidationMessageFor(model => model.AuctionTypeId)       </div>
        </section>
        <section>
                @Html.ValidationMessageFor(model => model.AutomaticExtensionType)       </div>
        </section>
}