【问题标题】:How to display error validation summary Header in ASP.Net MVC 5如何在 ASP.Net MVC 5 中显示错误验证摘要标题
【发布时间】: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


    【解决方案1】:

    您可以尝试添加如下的 css 规则,使标题错误部分最初不可见。

    验证摘要文本最初具有validation-summary-valid 类。如果有一些错误它会变成validation-summary-errors所以你的初始值没有任何错误,我认为你可以使用css规则

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

    【讨论】:

    • 但为什么我面临问题。我不明白。它是一种基本形式,我不应该遇到任何问题。真正的原因是什么。 :-|
    • 验证摘要文本最初有validation-summary-valid类,如果有一些错误它变成validation-summary-errors所以你的初始值没有任何错误,我认为你可以使用css规则
    • @Unbreakable 您的欢迎:)。请考虑接受答案以帮助人们了解问题已解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多