Html.ValidationSummary 显示重复的错误消息

发布于 2024-10-05 03:17:51 字数 406 浏览 0 评论 0原文

我已经搜索并谷歌搜索过这个问题的答案,但没有结果。我正在使用 EF4 和 ASP.NET MVC2,并且有一个 EF4 实体“Award”,其中包含一个不可为 null 的字符串字段“RecipientID”。我使用 DataAnnotations 进行服务器端验证,因此在我的“Award”部分类中,我将 RecipientID 设置为具有必需属性。当我尝试提交 RecipientID 文本框为空的表单时,我在 Html.ValidationSummary 中看到两次错误消息“请输入收件人”。

这是因为实体(因为它是具有空值的不可为空字段)和应用程序都抛出了错误吗?不管是什么原因,有没有办法“修复”这个问题并使错误消息只显示一次? (修复在引号中的问题,因为我不确定这是否是预期的行为。)我认为没有必要包含相关代码,但如果需要的话我会的。

预先感谢您的帮助。

I have searched and Googled for the answer to this question to no avail. I'm using EF4 and ASP.NET MVC2 and I have an EF4 entity "Award" with a non-nullable string field, "RecipientID". I'm using DataAnnotations for server-side validation, so in my "Award" partial class I've set up the RecipientID to have the Required attribute. When I try to submit the form with the RecipientID text box empty, I see my error message "Please enter a recipient" in the Html.ValidationSummary twice.

Would this be because the error is being thrown both by the entity (in that it is a non-nullable field with a null value), as well as the application? Whatever the reason, is there a way to "fix" this and have the error message show up only once? (Fix being in quotations because I'm not sure if this is intended behavior or not.) I didn't think it would be necessary to include relevant code, but I will if it's needed.

Thank you in advance for your help.

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

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

发布评论

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

评论(2

迟月 2024-10-12 03:17:52

我发现使用 ModelState.Clear();遇到类似问题时工作过。

I found that using ModelState.Clear(); worked when having a similar issue.

春夜浅 2024-10-12 03:17:51

看起来这是有意的,根据 布拉德·威尔逊。我应该搜索“ConstraintException”和 EF4。 :)

根据 Brad 的说法,在绑定模型之前会触发输入验证,从而导致抛出“必需”错误。此外,这些字段不可为空,这意味着它们在模型绑定期间抛出相同的“必需”。在我看来,它会显示“Required”属性中指定的相同消息,而不是 SQL 异常消息,这有点令人困惑,因为它使它看起来像相同的错误。但事实肯定不是这样。这就是 ConstraintException 的用武之地。为了防止重复消息,只需像这样包装模型绑定代码:

if (ModelState.IsValid) {
    ValidateModel(award);
    repository.Add(award);
    repository.Save();
}

就这么简单。谢谢!

It looks like this is intended, according to Brad Wilson. I should have searched for "ConstraintException" and EF4. :)

According to Brad, input validation is fired before the model is bound resulting in the "Required" error to be thrown. In addition, these fields are non-nullable, meaning they throw the same "Required" during model-binding. In my opinion, it's a bit confusing that it would show the same message specified in the "Required" attribute, rather than a SQL exception message, since it makes it look like the same error. Which it most certainly is not. That's where the ConstraintException comes in. In order to prevent the duplicate messages, simply wrap your model-binding code like so:

if (ModelState.IsValid) {
    ValidateModel(award);
    repository.Add(award);
    repository.Save();
}

Simple as that. Thanks!

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