Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
715 views
in Technique[技术] by (71.8m points)

asp.net mvc - ASP MVC jquery validation in bootsrap tabs causes an undesired postback

I have a form with 3 bootstrap tabs in it. Client Validation works correctly when the tab where the validation error occurs is open and submit button is clicked. However, when i switch to a tab with no errors (while having errors in other tabs) a post back occurs and i get the correct validation messages.

Its a minor issue, but post back is not desired in this situation, because full client side validation must be completed before sending request to the server.

How can i correct this behavior?

Below is a copy of my form 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")
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since two of you tabs are hidden when you submit the form, you need to configure the $.validator to validate hidden elements (which are not validated by default).

You current use of $.validator.setDefaults({ ignore: "" }); is not correct for jquery version 2.2.0 (I believe that usage was depreciated in version 1.9) and it needs to be

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

Note: do not add the above inside $(document).ready()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...