Custom Validation of MVC Models on IList

My model has an IList attribute. I need to write a special check to count this attribute (list), which should be more than two.

public IList<AccountAddress> BulkOrderAddresses { get; set; }

Please help me....

Thanks in advance

+3
source share
1 answer

You can implement the IValidatableObject interface

Something like that:

public class MyObject : IValidatableObject
{
     public IList<AccountAddress> BulkOrderAddresses { get; set; }

     public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
     {
          if(BulkOrderAddresses == null && !BulkOrderAddresses.Length > 2)
          {
               yield return new ValidationResult("List should contain more than 2 items");
          }
     }
}
+3
source

All Articles