如何显示验证摘要MVC

如何显示验证摘要MVC

本文介绍了如何显示验证摘要MVC 3客户端验证结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我用客户端验证登记表(必填,StringLength等我的视图模型指定)。目前的形式pretty许多棚架如何创建它:

I have a registration form on which I use client side validation (Required, StringLength etc. specified on my view model). The form is currently pretty much how the scaffolder creates it:

@using (Html.BeginForm("Index", "Registration"))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Registration details</legend>
        @Html.ValidationSummary(false, "Please correct these errors:")
        @Html.ValidationMessageFor(model => model.Username)
        <div class="editor-label">
            @Html.LabelFor(model => model.Username)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Username)
        </div>
        <p>
            <input type="submit" value="Register" />
        </p>
    </fieldset>
}

唯一的区别是,我感动到ValidationMessageFor在ValidationSummary下方的右上角。

The only difference is that I moved the ValidationMessageFor to the top right beneath the ValidationSummary.

我想这样做是在验证摘要显示客户端验证错误。目前他们只是显示在表格的顶部,但没有使用验证摘要。如何显示使用验证摘要客户端验证错误?这甚至可能?

What I would like to do is display the client side validation errors in the validation summary. Currently they are just displayed on top of the form but not using the validation summary. How can I display client side validation errors using the validation summary? Is this even possible?

更新

达林我用你的code在一个新的项目,这就是它看起来像我,当在客户端验证踢腿:

Darin I have used your code in a new project and this is what it looks like for me when the client side validation kicks in:

我希望这是显示在应用了验证摘要样式验证摘要。我还提交表单,然后是这样的:

I expected this to be shown IN the validation summary with the validation summary styles applied. I also submitted the form which then look like this:

谢谢,

b3n

推荐答案

此文章似乎解释了如何增加客户端验证总结到验证摘要:

This article seems to explain how add client side validation summary into the validation summary:

http://geekswithblogs.net/stun/archive/2011/01/28/aspnet-mvc-3-client-side-validation-summary-with-jquery-validation-unobtrusive-javascript.aspx

不过,我没有我自己测试它,它似乎并没有与你的code任何区别。如果它不工作的好角度去看待可能会在实际的地方验证错误的函数jquery.validate.unobtrusive.js文件:

However I did not test it by myself and it does not seem to have any difference with your code. If it does not work a good point to look might be jquery.validate.unobtrusive.js file on a function that actually places validation errors:

function onErrors(form, validator) {  // 'this' is the form element
    var container = $(this).find("[data-valmsg-summary=true]"),
        list = container.find("ul");

    if (list && list.length && validator.errorList.length) {
        list.empty();
        container.addClass("validation-summary-errors")
          .removeClass("validation-summary-valid");

        $.each(validator.errorList, function () {
            $("<li />").html(this.message).appendTo(list);
        });
    }
}

这篇关于如何显示验证摘要MVC 3客户端验证结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:14