【问题标题】:C# No validation when Hidden FieldC#隐藏字段时没有验证
【发布时间】:2014-10-29 08:49:44
【问题描述】:

以下代码应忽略任何具有类 ignore 的验证字段

使用 HTML 帮助器,我添加了 ignore 类,但每当我按下提交时,它仍然需要验证,即使添加了 ignore 类。

根据:jQuery disable rule validation on a single field 以下 jQuery 应该禁用对具有类 ignore 的字段的任何验证,但这不起作用。

jQuery

function hideBel() {
    $('#BelBep').hide();
    $('#iqform').validate(
       { ignore: ".ignore, :hidden"
    })
};

create.cshtml

<fieldset id="iqform">
    <legend>Form</legend>
    <div class="editor-label">@Html.LabelFor(model => model.BevBeleid) @Html.ValidationMessageFor(model => model.BevBeleid)</div>
    <label>@Html.RadioButtonFor(model => model.BevBeleid, "true", new { onclick = "showBel();"}) Ja</label>
    <label>@Html.RadioButtonFor(model => model.BevBeleid, "false", new { onclick = "hideBel();" }) Nee</label>

    <div id="BelBep" style="display: none;">
        <div class="editor-label">@Html.LabelFor(model => model.BelLastName)</div>
        <div class="editor-field">
            @Html.EditorFor(model => model.BelLastName, new { htmlAttributes = new {@class = "ignore"}})
            @Html.ValidationMessageFor(model => model.BelLastName)
        </div>

    </fieldset>

【问题讨论】:

    标签: c# jquery asp.net-mvc


    【解决方案1】:

    为了实现这种忽略行为,我们应该在 jQuery-validation-defaults 中添加必需的选择器。
    我们知道:hidden 是默认的,这里我们添加.ignore CssClass 选择器,如下所示。

    此默认脚本应如何以及放置在代码文件中的位置。

    我在 myview.cshtml 文件中执行如下操作

    <!--HTML and Razor markup -->
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    
        <script>
            //Below defaults setting should run 
            //after jquery.validation js execution and before DOM-Ready.
            //Note: This script block placement must be after below script embed tags
            //jquery.validate.min.js,jquery.validate.unobtrusive.min.js(=>jqueryval bundle)
            $.validator.setDefaults({
                ignore: ':hidden, .ignore'
            });
            $(function () {
                //Script to Run after DOM-Ready
            });
        </script>
    } 
    

    【讨论】:

    • 嗯,我要替换 $('#iqform').validate 吗?还是我将它添加到我的函数中,或者制作一个单独的函数。请延长一点!
    • 我尝试将它添加到该函数中,也在 document.ready 上的一个单独函数中,也尝试同时执行这两个函数.. 仍然要求验证
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多