问题描述
如果两个文本框的一次验证失败则在ValidationSummary显示两次相同的消息。
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:
查看:
@model MyModel
@Html.ValidationSummary()
@Html.TextBoxFor(model => model.A)
@Html.TextBoxFor(model => model.B)
型号:
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" });
}
}
结果:
推荐答案
它们不是从视图的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.
简单的解决方案:
- 分配模型只 其中之一
- 从汇总排除财产分配的错误 - Html.ValidationSummary(真)
一个有点困难的解决办法:
A little bit harder solution :
- 请自己的ValidationSummary帮手,叫标准验证摘要逻辑,然后筛选结果中选择不同的办法(LINQ这里是你的朋友)。
编辑:
这样的事情,例如:
public static class ValidationExtensions
{
public static MvcHtmlString FilteredValidationSummary(this HtmlHelper html)
{
// do some filtering on html.ViewData.ModelState
return System.Web.Mvc.Html.ValidationExtensions.ValidationSummary(html);
}
}
这篇关于的ValidationSummary显示重复的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!