加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net – 集合的MVC Dataannotation验证规则?

发布时间:2020-12-17 07:18:05 所属栏目:百科 来源:网络整理
导读:是否有基于集合的属性的数据注释验证规则? 我有以下内容 DisplayName("Category") Range(1,Integer.MaxValue,ErrorMessage:="Please select a category") Property CategoryId As Integer DisplayName("Technical Services") Property TechnicalServices As
是否有基于集合的属性的数据注释验证规则?

我有以下内容

<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;
    }    
}

还有改进的余地,但这是一个有效的开始.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读