我有以下的看法:
@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的注释,那么它将显示自定义服务器端错误,但它也将在标签下显示我想要的单个验证错误。有没有办法将验证摘要设置为忽略单独列出的值,而只返回服务器端验证错误?
发布于 2015-11-06 04:02:43
ModelState.AddModelError方法的第一个字符串参数是稍后用于将消息绑定到模型属性的键。
如果要在电子邮件标签旁边显示错误消息,请使AddModelError第一个参数与您的属性名匹配:
ModelState.AddModelError("Email", "Error! Email already used...");https://stackoverflow.com/questions/29935726
复制相似问题