ASP.NET MVC中MaxLength特性设置无效

在ASP.NET MVC项目中,给某个Model打上了MaxLength特性如下:

public class SomeClass
{
    [MaxLength(16, ErrorMessage = "最大长度16")]
    public string SomeProperty{get;set;}
}

但在其对应的表单元素中并没有出现类似data-val-length属性。

 

解决办法:使用StringLength替代MaxLength

public class SomeClass
{
    [StringLength(16, ErrorMessage = "最大长度16")]
    public string SomeProperty{get;set;}
}
原文地址:https://www.cnblogs.com/darrenji/p/4128169.html