[英]How to display error validation summary Header in ASP.Net MVC 5
我是ASP.Net MVC 5的初学者,我想知道如何在所有错误上方的页面顶部显示验证摘要“标题消息”。
以下是我到目前为止所拥有的:
视图:
@model WebApplication3.Models.Form
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Form</h4>
@* first parameter false means show all the errors *@
@* second parameter means the message to display as Header on top*@
@Html.ValidationSummary(false,"Fix below error", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "*", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.age, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.age, new { htmlAttributes = new { @class = "form-control" } })
@*star meaning show the star sign to keep show field required. *@
@Html.ValidationMessageFor(model => model.age, "*", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
@* CLIENT SIDE VALIDATION*@
@section Scripts
{
@Scripts.Render("~/bundles/jqueryval")
}
模型
public class Form
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
[Remote("IsAgeUnique", "Form", ErrorMessage = "Age is not unique")]
public int age { get; set; }
}
PS:我已经为每个属性使用@Html.ValidationMessageFor(prop, "*")
通配符,以在UI字段中显示星标消息。
问题:页面加载时,页面上已经显示了错误标题。 在功能方面,一切正常。 但是在初始页面加载期间,为什么显示“标题消息”
您可以尝试添加css规则,如下所示,以使标头错误部分最初不可见。
验证摘要文本最初具有validation-summary-valid类。 如果有一些错误,它将变为验证摘要错误,因此您的初始值没有任何错误,我认为您可以使用CSS规则
.validation-summary-valid {
display:none;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.