Monthly Archives: March 2016

Nonempty Collection Validation in ASP.NET MVC

using System;
using System.Collections;
using System.ComponentModel.DataAnnotations;

[AttributeUsage(AttributeTargets.Property)]
public sealed class NotEmptyAttribute : RequiredAttribute
{
    public override bool IsValid(object value)
    {
        ICollection valueCollection = value as ICollection;
        return valueCollection != null && valueCollection.Count > 0;
    }
}