MVC Dataannotation validation rule for a collection?

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.

Answers


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.


Need Your Help

linq.js groupby with Json

javascript jquery jquery-plugins linq.js

This is my Json Object in simplified form.

True Full Screen JFrame/Swing Application in Mac OSX

java osx swing jframe fullscreen

I'm working on application and I'm making the GUI with Swing. I would like my application to be fullscreen. I can easily set the size of the window however I have not been able to make the applic...