ValidationSummary 显示重复消息
如果两个文本框同时验证失败,则 ValidationSummary 会显示相同的消息两次。
我做错了什么吗?或者我可以更改设置来隐藏重复的消息?
我将其分解为最简单的示例:
View:
@model MyModel
@Html.ValidationSummary()
@Html.TextBoxFor(model => model.A)
@Html.TextBoxFor(model => model.B)
Model:
public class MyModel : IValidatableObject
{
public int A { get; set; }
public int B { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
//Some logic goes here.
yield return new ValidationResult("Validation failed", new[] { "A", "B" });
}
}
Result:
If two of textboxes fail validation at once then the ValidationSummary displays the same message twice.
Am I doing something wrong? Or is there a setting I can change to hide duplicate messages?
I have broken it down to the simplest example:
View:
@model MyModel
@Html.ValidationSummary()
@Html.TextBoxFor(model => model.A)
@Html.TextBoxFor(model => model.B)
Model:
public class MyModel : IValidatableObject
{
public int A { get; set; }
public int B { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
//Some logic goes here.
yield return new ValidationResult("Validation failed", new[] { "A", "B" });
}
}
Result:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 ValidationSummary 的角度来看,它们并不重复 - 您将模型状态错误分配给字段 A 和 B,因此验证摘要中必须有 2 个错误。它不“知道”它们是相同的。
简单的解决方案:
有点困难的解决方案:
编辑:
例如这样的事情:
They are not duplicate from the point of view of ValidationSummary - you are assigning model state error to both fields A and B, so there must be 2 errors in validation summary. It doesnt "know" that they are the same.
Easy solutions :
A little bit harder solution :
EDIT:
something like this for example :
这是你的视图,
你也许可以改进它,因为它只有在两个连续错误相同时才有效,如果它打乱了顺序,这可能不起作用,但这会让你开始。我想您可以构建一组错误消息并在每次运行时检查错误,但此解决方案似乎在大多数情况下都有效。
Whack this is your View
You might be able to improve this as it only works when 2 consecutive errors are the same, if it jumbles the order this might not work, but this will start you off. I suppose you could build an array of error messages and check the error off it each run through, but this solution seems to work most of the time.
ValidationSummary
方法返回属性级和模型级错误。如果您不指定任何参数,它只会枚举所有验证消息。你可以:
1) 对字段 A 和 B 使用不同的消息
,或者在您看来
2) 调用 ValidationSummary,并将 exceptPropertyErrors 参数设置为 true -
ValidationSummary(true)
。并将调用Html.ValidationMessage[For]
放在每个字段附近。更新:
...第三种情况:
在您的模型中添加公共消息(模型级别):
在您的视图中排除属性消息,但不要为字段添加 ValidationMessage:
因此您将得到一条消息和两个红色框。
ValidationSummary
method returns property-level and model-level errors. It just enumerates all validation messages if you don't specify any arguments.You can:
1) Use different message for field A and B
or, in your view
2) Call ValidationSummary with excludePropertyErrors argument set to true -
ValidationSummary(true)
. And place callHtml.ValidationMessage[For]
near each of your fields.UPDT:
... and third case:
In your model add common message (model-level):
In your view exclude property messages but don't add ValidationMessage for fields:
So you'll get single message and both red boxes.