Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.ComponentModel.DataAnnotations MaxLength not showing

I have created a separate class library to hold all my model objects. This is due to there being two sites, but needing the same model objects and validation that comes with it. Instead of copying and pasting the code in two places I thought I could just add the to a library. Everything looks great apart from MaxLength, it won't show up when I add System.ComponentModel.DataAnnotations

Yes, I have added a reference but nothing seems to get it to work like it does in a web solution.

like image 835
Funky Avatar asked Jul 30 '12 15:07

Funky


2 Answers

Try to use [StringLength(40, ErrorMessage = "Name cannot be longer than 40 characters.")] because this attribute generates client-side unobtrusive JS validation rules too.

like image 80
Ivan Manzhos Avatar answered Oct 19 '22 06:10

Ivan Manzhos


Try StringLength instead. StringLength is for model validation, whereas MaxLength is for Entity Framework to decide how large to make a string field in the database.

like image 28
Chad Avatar answered Oct 19 '22 05:10

Chad