【发布时间】:2018-01-11 13:12:32
【问题描述】:
我是 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; }
}
附注:
我为每个属性使用了@Html.ValidationMessageFor(prop, "*") 通配符,以在 UI 字段中显示星形消息。
问题: 当页面加载时,错误标题已经显示在页面上。功能方面一切正常。但是在初始页面加载期间,为什么会显示“标题消息”
【问题讨论】:
标签: c# asp.net asp.net-mvc validation asp.net-mvc-5