ValidationSummary 和 ValidationMessageFor,在没有错误时显示自定义 CSS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6899609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 01:08:53  来源:igfitidea点击:

ValidationSummary and ValidationMessageFor with custom CSS shown when no errors present

cssasp.net-mvc-3validation

提问by Vitalik

Once I applied custom CSS to ValidationSummary and ValidationMessageFor it appears on the page even when there are no errors. The CSS does have an image.

一旦我将自定义 CSS 应用于 ValidationSummary 和 ValidationMessageFor,即使没有错误,它也会出现在页面上。CSS 确实有一个图像。

How do I hide the summary and errors when there are no errors?

没有错误时如何隐藏摘要和错误?

@Html.ValidationSummary("", new { @class = "required" })
@Html.ValidationMessageFor(m => m.UserName, null, new { @class = "required" })


.required {
    background-image: url(/images/required.jpg);
    background-repeat: no-repeat;
    height: 33px;
    width: 386px;
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    font-size: 13px;
    font-weight: bold;
    color: #1eb4ec;
    padding-top: 16px;
    padding-left: 54px;
    margin-left: 30px;
}

回答by Chris Snowden

There are already built in css types for these:

已经为这些内置了 css 类型:

/* Styles for validation helpers
-----------------------------------------------------------*/
.field-validation-error {
    color: #ff0000;
}

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

.input-validation-error {
    border: 1px solid #ff0000;
    background-color: #ffeeee;
}

.validation-summary-errors {
    font-weight: bold;
    color: #ff0000;
}

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

in the default css file for MVC3 projects. These are shown appropriately based on if the ModelStatehas any errors for the Propertyspecified or any at all for ValidationSummary. If no errors are present in the ModelStatethen the display:none;from .field-validation-validwill hide the errors.

在 MVC3 项目的默认 css 文件中。这些将根据指定的ModelState错误Property或根本错误显示ValidationSummary。如果from 中不存在错误,ModelStatedisplay:none;from.field-validation-valid将隐藏错误。

回答by stack72

We don't put a class of required on our ValidationMessagesFor helpers

我们没有在我们的 ValidationMessagesFor helpers 上放置一个 required 类

<li>
                    <label for="NameFirst">First Name <span>* Required</span></label>
                    <span class="field-container">
                        @Html.TextBoxFor(m => m.FirstName, new { required = "required", data_type="name" })
                        @Html.ValidationMessageFor(m => m.FirstName)
                    </span>
                </li>