VB.NET中如何实现MVC模型中的数据注解验证规则?

2026-05-06 10:182阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计235个文字,预计阅读时间需要1分钟。

VB.NET中如何实现MVC模型中的数据注解验证规则?

是否有基于集合的属性的 数据注释验证规则?

我有以下内容:DisplayName(Category)Range(1, Integer.MaxValue, ErrorMessage=Please select a category)PropertyCategoryId As IntegerDisplayName(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; } }

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

VB.NET中如何实现MVC模型中的数据注解验证规则?

本文共计235个文字,预计阅读时间需要1分钟。

VB.NET中如何实现MVC模型中的数据注解验证规则?

是否有基于集合的属性的 数据注释验证规则?

我有以下内容:DisplayName(Category)Range(1, Integer.MaxValue, ErrorMessage=Please select a category)PropertyCategoryId As IntegerDisplayName(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; } }

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

VB.NET中如何实现MVC模型中的数据注解验证规则?