asp.net mvc 3验证摘要未通过不显眼的验证显示

Sta*_*anK 12 unobtrusive-validation asp.net-mvc-3

我在使用asp.net MVC客户端验证时遇到了问题.

我基本上可以使用它,但是,在用户单击提交按钮之前不会显示验证摘要,即使单个输入被突出显示为无效,因为用户选项卡/单击等通过表单.这一切都发生在客户端.

我会想到,一旦发现输入字段无效,就会显示验证摘要.

这种行为是设计的吗?是否有任何解决方法,因为我希望一旦发现其中一个输入字段无效就会显示验证摘要.

我的代码基本上是,

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
...
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(false)
        @Html.EditorFor(model => model);   
        ...
Run Code Online (Sandbox Code Playgroud)

我的_Layout.cshtml参考资料jquery-1.4.4.min.js.

Tod*_*rst 5

我使用的是TorbjörnNomells的版本

除了这里,我将resetSummary挂起了验证器对象

$.validator.prototype.resetSummary= function () {
    var form = $(this.currentForm);
    form.find("[data-valmsg-summary=true]")
        .removeClass("validation-summary-errors")
        .addClass("validation-summary-valid")
        .find("ul")
        .empty();
    return this;
};
Run Code Online (Sandbox Code Playgroud)

然后将其更改为

$.validator.setDefaults({
    showErrors: function (errorMap, errorList) {
        this.defaultShowErrors();
        this.checkForm();
        if (this.errorList.length) {
            $(this.currentForm).triggerHandler("invalid-form", [this]);
        } else {
            this.resetSummary();
        }
    } 
});
Run Code Online (Sandbox Code Playgroud)


Tor*_*ell 4

您可以在 onready 中设置更频繁地触发验证摘要:

var validator = $('form').data('validator');
validator.settings.showErrors = function (map, errors) {
    this.defaultShowErrors();
    this.checkForm();
    if (this.errorList.length)
        $(this.currentForm).triggerHandler("invalid-form", [this]);
    else
        $(this.currentForm).resetSummary();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是上面使用的resetSummary:

jQuery.fn.resetSummary = function () {
    var form = this.is('form') ? this : this.closest('form');
    form.find("[data-valmsg-summary=true]")
        .removeClass("validation-summary-errors")
        .addClass("validation-summary-valid")
        .find("ul")
        .empty();
    return this;
};
Run Code Online (Sandbox Code Playgroud)