在验证摘要中仅显示服务器端错误

时间:2015-04-29 06:14:49

标签: validation razor asp.net-mvc-5 modelstate validationsummary

我有以下观点:

@model MyProject.Models.RegisterViewModel

<div class="content">
<div class="wrapper">
    <h2>E-Bill Saver Registration</h2>
    <hr />

    <div class="columns top-gap">
        <div class="first cell width-6 dark-blue-headings">
            @using (Html.BeginForm())
            {
                @Html.AntiForgeryToken()
                @*@Html.ValidationSummary("", new { @class = "text-danger" })*@
                <div class="form-horizontal">                        
                    <div class="form-group">
                        @Html.Label("Account Number", htmlAttributes: new { @class = "control-label col-md-2 bold" })
                        <br />@Html.ValidationMessageFor(model => model.AccountNo, "", new { @class = "text-danger" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.AccountNo, new { htmlAttributes = new { @class = "form-control input" } })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.Label("MPNumber", htmlAttributes: new { @class = "control-label col-md-2 bold" })
                        <br />@Html.ValidationMessageFor(model => model.MPNumber, "", new { @class = "text-danger" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.MPRN, new { htmlAttributes = new { @class = "form-control input" } })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.Label("Email Address", htmlAttributes: new { @class = "control-label col-md-2 bold" })
                        <br />@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control input" } })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.Label("Confirm Email Address", htmlAttributes: new { @class = "control-label col-md-2 bold" })
                        <br />@Html.ValidationMessageFor(model => model.ConfirmEmail, "", new { @class = "text-danger" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.ConfirmEmail, new { htmlAttributes = new { @class = "form-control input" } })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.Label("Password", htmlAttributes: new { @class = "control-label col-md-2 bold" })
                        <br />@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control input" } })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.Label("Confirm Password", htmlAttributes: new { @class = "control-label col-md-2 bold" })
                        <br />@Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { @class = "form-control input" } })
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" value="Register" class="opc_button" />
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>       
</div>

如果任何字段上存在验证错误,则错误消息将按预期和需要显示在标签下方。然而,在我的控制器上,我有一些自定义逻辑错误,我将它们添加到模型状态,如下所示:

else
                {
                    //This email address is already assigned to a user      
                    ModelState.AddModelError("Email Address : ", "Error! You must enter an email address that is unique to your account.");
                    return View(model);
                }

如果我取消注释页面上的validationsummary,那么它将显示自定义服务器端错误,但它也会在标签下方显示我想要的单个验证错误。有没有什么办法可以设置验证摘要来忽略单独列出的值,只返回服务器端验证错误?

1 个答案:

答案 0 :(得分:0)

ModelState.AddModelError方法的第一个字符串参数是稍后用于将消息绑定到模型属性的键 如果要在电子邮件标签旁边显示该错误消息,请将AddModelError的第一个参数与您的媒体资源名称相匹配:

ModelState.AddModelError("Email", "Error! Email already used...");