Asp.net mvc bootsrap选项卡中的ASP MVC jquery验证会导致不需要的回发

Asp.net mvc bootsrap选项卡中的ASP MVC jquery验证会导致不需要的回发,asp.net-mvc,twitter-bootstrap-3,jquery-validation-engine,Asp.net Mvc,Twitter Bootstrap 3,Jquery Validation Engine,我有一个表单,里面有3个引导选项卡客户端验证在出现验证错误的选项卡打开并单击提交按钮时正常工作。但是,当我切换到一个没有错误的选项卡(而其他选项卡中有错误)时,会发生回发,我会得到正确的验证消息 这是一个小问题,但在这种情况下不需要回发,因为在向服务器发送请求之前必须完成完整的客户端验证 如何纠正这种行为 以下是我的HTML表单副本: @model RBZPOSMVC.ViewModel.CreateEditItem .... @using (Html.BeginForm()) { @

我有一个表单,里面有3个
引导选项卡
<代码>客户端验证在出现
验证错误
的选项卡打开并单击
提交按钮时正常工作。但是,当我切换到一个没有错误的选项卡(而其他选项卡中有错误)时,会发生
回发
,我会得到正确的验证消息

这是一个小问题,但在这种情况下不需要回发,因为在向服务器发送请求之前必须完成完整的客户端验证

如何纠正这种行为

以下是我的HTML表单副本:

@model RBZPOSMVC.ViewModel.CreateEditItem
....
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <ul class="nav nav-tabs">
            <li class="active"><a data-toggle="tab" href="#Details">Item Details</a></li>
            <li><a data-toggle="tab" href="#Sales">Sale Price Groups</a></li>
            <li><a data-toggle="tab" href="#Purchases">Purchase Price Groups</a></li>
        </ul>

        <div class="tab-content">
            <div id="Details" class="tab-pane fade in active">
                <div class="form-group">
                    @Html.LabelFor(model => model.Item.ItemCode, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.Item.ItemCode, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Item.ItemCode, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.Item.ItemName, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.Item.ItemName, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Item.ItemName, "", new { @class = "text-danger" })
                    </div>
                </div>
                .... // more form controls
            </div>
            <div class="hidden">
                @for (var i = 0; i < Model.PriceGroupList.Count; i++)
                {
                    <div class="form-group" id="@Model.PriceGroupList[i].PriceGroupTypeId">
                        @Html.HiddenFor(model => model.PriceGroupList[i].PriceGroupId)
                        @Html.LabelFor(model => model.PriceGroupList[i].PriceGroupName,
                                                   Model.PriceGroupList[i].PriceGroupName,
                                                htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.PriceGroupList[i].Price, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.PriceGroupList[i].Price, "", new { @class = "text-danger" })
                        </div>
                    </div>
                }
            </div>
            <div id="Sales" class="tab-pane fade in ">
            </div>
            <div id="Purchases" class="tab-pane fade in ">
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
<script>
    $.validator.setDefaults({
        ignore: ""
    });

</script>

    @Scripts.Render("~/bundles/items")
    @Scripts.Render("~/bundles/jqueryval")
}
@model RBZPOSMVC.ViewModel.CreateEditItem
....
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.Item.ItemCode,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Item.ItemCode,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Item.ItemCode,”,新的{@class=“text danger”}) @LabelFor(model=>model.Item.ItemName,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Item.ItemName,new{htmlAttributes=new{@class=“form control”}}) @Html.ValidationMessageFor(model=>model.Item.ItemName,“,new{@class=“text danger”}) ..//更多表单控件 @对于(var i=0;imodel.PriceGroupList[i].PriceGroupId) @Html.LabelFor(model=>model.PriceGroupList[i].PriceGroupName, Model.PriceGroupList[i].PriceGroupName, htmlAttributes:new{@class=“控制标签col-md-2”}) @EditorFor(model=>model.PriceGroupList[i].Price,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.PriceGroupList[i].Price,“,new{@class=“text danger”}) } } @ActionLink(“返回列表”、“索引”) @节脚本{ $.validator.setDefaults({ 忽略:“ }); @Scripts.Render(“~/bundles/items”) @Scripts.Render(“~/bundles/jqueryval”) }
由于提交表单时两个选项卡是隐藏的,因此需要配置
$.validator
来验证隐藏元素(默认情况下不会验证)

您当前使用的
$.validator.setDefaults({ignore:”“})对于jquery版本2.2.0是不正确的(我认为在版本1.9中使用率被降低了),需要修改它

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

注意:不要在
$(document.ready()

包中包含哪些文件?
“~/bundles/jqueryval”
包中包含哪些文件?我使用的是标准MVC模板,因此它有
jquery.validate.min.js
jquery.validate.unobtrusive.min.js
。基本上是这样的:
bundles.Add(newscriptbundle(“~/bundles/jqueryval”)。包括(“~/Scripts/jquery.validate*”)
根据引导选项卡的版本,您可能需要处理
显示的
事件-例如
$('a[data toggle=“tab”]')。on('show',function(e){form.data('validator',null);…})我尝试了你的解决方案。它不起作用,我编辑了问题。它对动态添加的内容很有效(但正如我所指出的,正确的语法取决于版本)。稍后我将创建一个
DotNetFiddle
来测试它,但在此期间,您是否对它进行了调试,以确保您实际命中了
ParseDynamicContent()
方法,并且在控制台中没有收到任何错误?确实命中了
ParseDynamicContent()
。根据其中一个答案,它的
$('a[data toggle=“tab”]”)上('show.bs.tab',函数(e){
用于版本3