我被MSDN示例弄糊涂了。
还不清楚如何处理和设置实体重新生成的错误。
代码示例:
public System.Collections.IEnumerable GetErrors(string propertyName)
{
if (String.IsNullOrEmpty(propertyName) ||
!errors.ContainsKey(propertyName)) return null;
return errors[propertyName];
}但是GetErrors()的文档声明:
propertyName -要检索验证错误的属性的名称;或null或空,用于检索实体级错误。
另一个例子建议只返回字典的_errors.Values。这只是所有属性错误,但也不是实体错误。
发布于 2013-06-18 07:17:40
根据文档中的“备注”部分:MSDN: INotifyDataErrorInfo接口
此接口使数据实体类能够实现自定义验证规则并异步公开验证结果。此接口还支持自定义错误对象、每个属性多个错误、跨属性错误和实体级错误。交叉属性错误是影响多个属性的错误。您可以将这些错误与一个或所有受影响的属性关联,也可以将它们视为实体级错误。实体级错误是影响多个属性或影响整个实体而不影响特定属性的错误。
我可能会建议,GetErrors的实现高度依赖于您的错误处理方案。例如,如果您不打算支持Entity-Level错误,那么示例代码就足够了。但是,如果确实需要支持Entity-Level错误,则可以单独处理IsNullOrEmpty条件:
Public IEnumerable GetErrors(String propertyName)
{
if (String.IsNullOrEmpty(propertyName))
return entity_errors;
if (!property_errors.ContainsKey(propertyName))
return null;
return property_errors[propertyName];
}发布于 2019-02-03 14:25:50
由于我在这里没有找到正确的答案,所以当值为null或空时,将返回所有验证错误:
private ConcurrentDictionary<string, List<ValidationResult>> modelErrors = new ConcurrentDictionary<string, List<ValidationResult>>();
public bool HasErrors { get => modelErrors.Any(); }
public IEnumerable GetErrors(string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
{
return modelErrors.Values.SelectMany(x => x); // return all errors
}
modelErrors.TryGetValue(propertyName, out var propertyErrors);
return propertyErrors;
}https://stackoverflow.com/questions/15874453
复制相似问题