首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在验证摘要中仅显示服务器端错误

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

Stack Overflow用户
提问于 2015-04-29 14:14:49
回答 1查看 1.6K关注 0票数 0

我有以下的看法:

代码语言:javascript
复制
@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>

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

代码语言:javascript
复制
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的注释,那么它将显示自定义服务器端错误,但它也将在标签下显示我想要的单个验证错误。有没有办法将验证摘要设置为忽略单独列出的值,而只返回服务器端验证错误?

EN

回答 1

Stack Overflow用户

发布于 2015-11-06 04:02:43

ModelState.AddModelError方法的第一个字符串参数是稍后用于将消息绑定到模型属性的键。

如果要在电子邮件标签旁边显示错误消息,请使AddModelError第一个参数与您的属性名匹配:

代码语言:javascript
复制
ModelState.AddModelError("Email", "Error! Email already used...");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29935726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档