【问题标题】:ValidationSummary and ValidationMessageFor with custom CSS shown when no errors present没有错误时显示自定义 CSS 的 ValidationSummary 和 ValidationMessageFor
【发布时间】:2011-08-01 14:06:47
【问题描述】:

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

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

@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;
}

【问题讨论】:

    标签: css asp.net-mvc-3 validation


    【解决方案1】:

    这些已经内置了 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;
    }
    

    在 MVC3 项目的默认 css 文件中。这些将根据ModelState 是否对指定的Property 有任何错误或对ValidationSummary 有任何错误进行适当显示。如果ModelState 中没有错误,则来自.field-validation-validdisplay:none; 将隐藏错误。

    【讨论】:

    • 完美,我修改了内置的css样式。谢谢
    【解决方案2】:

    我们没有在我们的 ValidationMessagesFor 助手上放置一个 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 2020-01-27
      相关资源
      最近更新 更多