jquery验证未显示错误消息

时间:2015-06-19 05:08:30

标签: javascript jquery asp.net-mvc jquery-ui

这是我的脚本代码,当我运行此代码时,它没有显示任何消息,它只显示必填字段的红色texbox但我想在文本框下方显示错误消息

(function ($, W, D) {
    var JQUERY4U = {};

    JQUERY4U.UTIL =
    {
        setupFormValidation: function () {
            $("#register-form").validate({
                rules: {
                    FirstName: "required",
                    LastName: "required",
                },
                messages: {
                    FirstName: "Please enter your firstname",
                    LastName: "Please enter your lastname",
                },
                submitHandler: function (form) {
                    form.submit();
                }
            });
        }
    }
    $(D).ready(function ($) {
        JQUERY4U.UTIL.setupFormValidation();
    });

})(jQuery, window, document);

这是我的观看代码

<form action="" method="post" id="register-form" novalidate="novalidate">
    <div class="col-md-6">
        <div class="panel panel-default nobottommargin">
            <div class="panel-body" style="padding: 40px;">
                <div class="heading-block fancy-title nobottomborder title-bottom-border">
                    <h4>Registration</h4>
                </div>
                @using (Html.BeginForm("Registration", "Account"))
                                            {
                    @Html.AntiForgeryToken()

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

                        <div class="form-group">
                            @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-4" })
                            <div class="col-md-8">
                                @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })

                                @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
                            </div>
                        </div>
                        <div class="form-group">
                            @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-4" })
                            <div class="col-md-8">
 //this is model binding in mvc                                                               
                                @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })

                                @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
                            </div>
                        </div>

2 个答案:

答案 0 :(得分:0)

查看内使用

@Scripts.Render("~/bundles/jqueryval")

这将引用将处理客户端验证的jquery.validate javascript。

模型类中,使用 [必需] 属性作为类属性。

[Required]
public string FirstName{ get; set; }

然后在控制器中 如果ModelState无效/不符合您的要求,则返回模型。

if (!ModelState.IsValid)
{
     return View(model);
}

答案 1 :(得分:0)

  

仅显示红色文本框而不显示消息

那是因为你告诉它只显示自定义错误而不显示属性错误

更改

@Html.ValidationSummary(true, "", new { @class = "text-danger" })

@Html.ValidationSummary(false, "", new { @class = "text-danger" })

然后你会在摘要框和@Html.ValidationMessageFor中得到属性错误 - 所以你应该删除那些

然而

如果你也没有在@Html.ValidationMessageFor中收到属性错误,那么你没有连接你的验证用于这两个MVC助手 - 你期望这些做他们不做的事情。

所有验证都应 客户端和服务器端。通过在viewmodel上应用服务器端属性([Required])并使用unobtrusive.js,您将自动获得两者,而无需两次写入。