是否有基于集合的属性的数据注释验证规则? 我有以下内容 DisplayName("Category") Range(1, Integer.MaxValue, ErrorMessage:="Please select a category") Property CategoryId As Integer DisplayName("Technical Services") Pr
我有以下内容
<DisplayName("Category")>
<Range(1, Integer.MaxValue, ErrorMessage:="Please select a category")>
Property CategoryId As Integer
<DisplayName("Technical Services")>
Property TechnicalServices As List(Of Integer)
我正在寻找一个验证器,我可以添加到TechnicalServices属性来设置集合大小的最小值.
我认为这样的事情可能会有所帮助:public class MinimumCollectionSizeAttribute : ValidationAttribute
{
private int _minSize;
public MinimumCollectionSizeAttribute(int minSize)
{
_minSize = minSize;
}
public override bool IsValid(object value)
{
if (value == null) return true;
var list = value as ICollection;
if (list == null) return true;
return list.Count >= _minSize;
}
}
还有改进的余地,但这是一个有效的开始.
