对于集合MVC Dataannotation验证规则? [英] MVC Dataannotation validation rule for a collection?
本文介绍了对于集合MVC Dataannotation验证规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一个基于集合属性dataannotation验证规则?
Is there a dataannotation validate rule for a collection based property?
我有以下
<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属性设置为集合尺寸最小的验证。
I'm looking for a validator that i can add to the TechnicalServices property to set a minimum for the collection size.
推荐答案
我觉得这样的事情可能会有所帮助:
I think something like this might help:
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;
}
}
有改进的空间,但是这是一个工作的开始。
There's room for improvement, but that's a working start.
这篇关于对于集合MVC Dataannotation验证规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文