简体   繁体   English

如何在ASP.Net MVC 5中显示错误验证摘要标题

[英]How to display error validation summary Header in ASP.Net MVC 5

I am a beginner in ASP.Net MVC 5 and I want to know how to display validation summary "Header Message" on top of page above all the errors. 我是ASP.Net MVC 5的初学者,我想知道如何在所有错误上方的页面顶部显示验证摘要“标题消息”。

Below is what I have till now: 以下是我到目前为止所拥有的:

View: 视图:

@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")
 }

Model 模型

 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: I have used @Html.ValidationMessageFor(prop, "*") wildcard for each property to display star message side to the UI field. PS:我已经为每个属性使用@Html.ValidationMessageFor(prop, "*")通配符,以在UI字段中显示星标消息。

Issue: When the page loads the error header is already there displayed on the page. 问题:页面加载时,页面上已经显示了错误标题。 Functionality wise everything is working fine. 在功能方面,一切正常。 But during initial page load why the "Header message gets displayed" 但是在初始页面加载期间,为什么显示“标题消息”

在此处输入图片说明

You can try to add css rule like following to make header error part unvisible initially. 您可以尝试添加css规则,如下所示,以使标头错误部分最初不可见。

Validation summary text has validation-summary-valid class initially. 验证摘要文本最初具有validation-summary-valid类。 If there are some errors it becomes validation-summary-errors so your initial value dont have any error, I think you can use css rule 如果有一些错误,它将变为验证摘要错误,因此您的初始值没有任何错误,我认为您可以使用CSS规则

.validation-summary-valid {
    display:none;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM