Asp.net mvc dataannotation MaxLength验证不起作用

时间:2022-02-18 16:39:04

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

我在我的模型中使用asp.net mvc4我使用Maxlength属性但它不适用于字符串。只有Stringlength工作任何人都有同样的问题?如果有问题如何解决?它不适用于验证我的字段这是我的代码

(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; }

3 个解决方案

#1


28  

Both Attributes are in System.ComponentModel.DataAnnotations namespace

两个属性都在System.ComponentModel.DataAnnotations命名空间中

As per Microsoft Official Website [MaxLenth] 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))

根据实体框架中的Microsoft官方网站[MaxLenth]属性,因为实体框架知道在您的情况下数据库中列的最大长度(如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

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

正如你们其中一个评论你说你没有使用实体框架回复@jackncoke所以[MaxLength(80)]将无法正常工作

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

但在第二种情况下[StringLength(80)]正在工作,因为它对Entity Framework没有任何依赖性。

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

如果您正在使用Entity Framework或没有它,SO [StringLength(80)]将适用于这两种情况

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

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

#2


6  

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

[MaxLength(80)]改为[StringLength(80)],但看起来你打败了我!

Your not the only one with this problem

你不是唯一有这个问题的人

MaxLength Attribute not generating client-side validation attributes

MaxLength属性不生成客户端验证属性

#3


3  

IN MVC4 MaxLength workd properly, i have to check it

在MVC4 MaxLength正常工作,我必须检查它

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; }
}

Asp.net mvc dataannotation MaxLength验证不起作用

#1


28  

Both Attributes are in System.ComponentModel.DataAnnotations namespace

两个属性都在System.ComponentModel.DataAnnotations命名空间中

As per Microsoft Official Website [MaxLenth] 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))

根据实体框架中的Microsoft官方网站[MaxLenth]属性,因为实体框架知道在您的情况下数据库中列的最大长度(如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

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

正如你们其中一个评论你说你没有使用实体框架回复@jackncoke所以[MaxLength(80)]将无法正常工作

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

但在第二种情况下[StringLength(80)]正在工作,因为它对Entity Framework没有任何依赖性。

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

如果您正在使用Entity Framework或没有它,SO [StringLength(80)]将适用于这两种情况

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

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

#2


6  

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

[MaxLength(80)]改为[StringLength(80)],但看起来你打败了我!

Your not the only one with this problem

你不是唯一有这个问题的人

MaxLength Attribute not generating client-side validation attributes

MaxLength属性不生成客户端验证属性

#3


3  

IN MVC4 MaxLength workd properly, i have to check it

在MVC4 MaxLength正常工作,我必须检查它

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; }
}

Asp.net mvc dataannotation MaxLength验证不起作用