MVC 集合的数据注释验证规则?

发布于 2024-10-06 05:54:10 字数 370 浏览 0 评论 0原文

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

我有以下内容,

  <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 属性中以设置集合大小的最小值。

Is there a dataannotation validate rule for a collection based property?

I have the following

  <DisplayName("Category")>
  <Range(1, Integer.MaxValue, ErrorMessage:="Please select a category")>
  Property CategoryId As Integer

  <DisplayName("Technical Services")>
  Property TechnicalServices As List(Of Integer)

I'm looking for a validator that i can add to the TechnicalServices property to set a minimum for the collection size.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜尕妞 2024-10-13 05:54:10

我认为这样的事情可能会有所帮助:

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

还有改进的空间,但这是一个工作的开始。

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.

阳光下慵懒的猫 2024-10-13 05:54:10

从 .NET 4 开始,另一个选择是让类本身(包含有问题的集合属性)实现 IValidatableObject,例如:

Public Class SomeClass
  Implements IValidatableObject

  Public Property TechnicalServices() As List(Of Integer)
        Get
            Return m_TechnicalServices
        End Get
        Set
            m_TechnicalServices = Value
        End Set
    End Property
    Private m_TechnicalServices As List(Of Integer)

    Public Function Validate(validationContext As ValidationContext) As IEnumerable(Of ValidationResult)
        Dim results = New List(Of ValidationResult)()

        If TechnicalServices.Count < 1 Then
            results.Add(New ValidationResult("There must be at least one TechnicalService"))
        End If

        Return results
    End Function
End Class

Validator 会自动为任何 IValidatableObjects 调用此 Validate 方法。

Another option from .NET 4 onward would be to make the class itself (which contains the collection property in question) implement IValidatableObject, such as:

Public Class SomeClass
  Implements IValidatableObject

  Public Property TechnicalServices() As List(Of Integer)
        Get
            Return m_TechnicalServices
        End Get
        Set
            m_TechnicalServices = Value
        End Set
    End Property
    Private m_TechnicalServices As List(Of Integer)

    Public Function Validate(validationContext As ValidationContext) As IEnumerable(Of ValidationResult)
        Dim results = New List(Of ValidationResult)()

        If TechnicalServices.Count < 1 Then
            results.Add(New ValidationResult("There must be at least one TechnicalService"))
        End If

        Return results
    End Function
End Class

The Validator in DataAnnotations will automatically call this Validate method for any IValidatableObjects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文