Validating Dictionary ?

Oct 26, 2011 at 11:26 PM


this is one of the best libraries i have ever used to ease up the work, thanks alot for giving it to us :)

Validation works fine on strings, etc... but i am running through Issue here, i cant seem to validate a Dictionary

here is my code


Check(c => c.NameCol).Required("Please add a Name");
Check(c => c.Name).If(c => c.NameCol.Count == 0).Required("Please add a Name");
these are my Model Properties


public string Name { getset; }
 public Dictionary<stringstringNameCol { getset; }

even if i try to validate the name based on the dictionaries is empty or not i always get  model state invlaid in ASP.MVC 3

thanks in advanced for the help.
Oct 27, 2011 at 12:27 AM

Hi DevMondo,

Thanks for the great compliments! 

The Required rule will validate that the property is not the default value.  Since a Dictionary is an object type, and all object types have a default value of null, as long as the NamCol property is not null, the Required will succeed.  I believe what your looking for is CountGreaterThan.  Of course you want to make sure that the value is not null so you still need the Required.  So the Check expresion your looking for would look something like this:

Check(c => c.NameCol).Required().CountGreaterThan(0);


Oct 27, 2011 at 1:36 AM


thanks alot for the prompt reply it, it worked like charm :)

anywhere we can donate for the project ?