System.ComponentModel.DataAnnotations MaxLength not showing

12,753

Solution 1

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

Solution 2

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.

Share:
12,753
Funky
Author by

Funky

Updated on June 22, 2022

Comments

  • Funky
    Funky about 2 years

    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.