21

I am using asp.net mvc4 in my model i am using Maxlength attribute but it does not work for string. Only Stringlength works anyone have same problem ? if had issue how to resolve ? it does not work for validate my field Here is my code

(not working)

[Required]
[MaxLength(80)]
[DisplayName("Contact Name:")]
public string ContactName { get; set; }

(working)

[Required]
[StringLength(80)]
[DisplayName("Contact Name:")]
public string ContactName { get; set; }
Garry
  • 4,996
  • 5
  • 32
  • 44

3 Answers3

37

Both Attributes are in System.ComponentModel.DataAnnotations namespace

As per Microsoft Official Website [MaxLength] attribute in for Entity Framework because Entity Framework knows what can be the maximum length of the column in the database in your case(like varchar(80))

Specifies the maximum length of array or string data allowed in a property.

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.maxlengthattribute.aspx

As in one of you comment you have said you are not using Entity Framework in reply with @jackncoke so [MaxLength(80)] will not work

But in second case [StringLength(80)] is working because it does not have any dependency over Entity Framework.

SO [StringLength(80)] will work in both cases if you are using Entity Framework or without it

Specifies the minimum and maximum length of characters that are allowed in a data field.

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute.aspx

Offir
  • 3,252
  • 3
  • 41
  • 73
Pamma
  • 1,455
  • 9
  • 16
6

[MaxLength(80)] change to [StringLength(80)] but looks like you beat me to it!

Your not the only one with this problem

MaxLength Attribute not generating client-side validation attributes

AlexB
  • 7,302
  • 12
  • 56
  • 74
jackncoke
  • 2,000
  • 6
  • 25
  • 66
  • 1
    yes i know it works with [StringLength(80)] but my question is why not with [MaxLength(80)] ? Even accoridng to microsoft it is valid attibute but you are the first first guy for answer so +1 for you – Garry Jan 31 '13 at 15:23
  • 1
    I was just looking through old projects and I have never used it always used StringLength. So I been looking on the internet to see if anyone else had this sort of issue. I have seen people use it as i been searching. – jackncoke Jan 31 '13 at 15:41
  • What version of EF are you using? – jackncoke Jan 31 '13 at 15:44
  • 1
    I have not used entity framework i am passing static data to my model..And it should not make any difference .I was curious if it does not work only for me or for others too.. – Garry Jan 31 '13 at 15:47
3

IN MVC4 MaxLength workd properly, i have to check it

public class RegisterModel
{
    [Required]
    [Display(Name = "User name")]
    [MaxLength(5)]  //MaxLength worked properly.
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

enter image description here