如何停止 ValidationMessageFor 在加载时显示错误消息?

发布于 2024-12-13 17:59:26 字数 344 浏览 5 评论 0原文

我已将验证消息“CountryId 自定义错误”添加到视图中。问题是当我最初加载视图以及 POST 时它都会显示。

<div class="editor-field">
@Html.DropDownList("CountryId", Model.GetCountries, "Please select", "0")
@Html.ValidationMessageFor(m => m.CountryId, "CountryId custom error")
</div>

如何让它只显示在 POST 上? 如果我省略验证消息,视图将正常运行,并在 POST 上显示默认错误。

I have added a validation message "CountryId custom error" to a view. Problem is it displays when I load the view initially as well as on POST.

<div class="editor-field">
@Html.DropDownList("CountryId", Model.GetCountries, "Please select", "0")
@Html.ValidationMessageFor(m => m.CountryId, "CountryId custom error")
</div>

How can I make it only display on the POST?
If I omit the validation message the view behaves properly showing a default error on POST.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2024-12-20 17:59:26

该项目是作为一个空的 MVC 应用程序创建的,我删除了 site.css 并重新开始。
如果您希望验证正常工作,则不能这样做,因为 main.css 中有重要的 css 隐藏了消息!

哎呀!!

This project was created as an empty MVC app and I deleted the site.css and started again.
You can't do this if you want the validation to work as there is vital css in main.css that hides the messages!

Doh!!

清风夜微凉 2024-12-20 17:59:26

以下是 ValidationMessageFor 使用的 CSS 类。从其他项目复制 CSS 时很容易丢失这些内容。

    .field-validation-error {
        color: red;
        font-weight: bold;
    }

    .input-validation-error {
        background: pink;
    }

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

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

Here are the CSS classes used by ValidationMessageFor. It's easy to lose these when copying over CSS from other projects.

    .field-validation-error {
        color: red;
        font-weight: bold;
    }

    .input-validation-error {
        background: pink;
    }

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

    .field-validation-valid {
        display: none;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文