MVC隐藏字段被验证 [英] MVC hidden field being validated

查看:131
本文介绍了MVC隐藏字段被验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有出现,dissappear取决于下拉选择您在页面上我的网页上的几个领域。

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

因此​​,例如,我有

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

jQuery的code我对这个是

The JQuery code I have for this is

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

现在,在显示和隐藏工作应该像他们。问题是,当我提交表单和字段 automaticExtensionTypeList 是隐藏的,形式不提交,因为 automaticExtensionTypeList 为必填字段。现在的问题是我怎么能告诉MVC只验证可见字段?

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

我已经通过一些JQuery的,我们已经写在这个项目中一看,我们这行

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

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

Apparantly这使隐藏的验证。我的问题是,什么线code的恰好相反?

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

推荐答案

试试这个:

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

或定制

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

这篇关于MVC隐藏字段被验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆