[MaxLength]
和[StringLength]
属性的行为有什么不同?
据我所知(除了[MaxLength]
可以验证数组的最大长度之外),它们是相同的并且有点冗余吗?
发布于 2017-01-06 22:51:47
以下是在EF code first
中同时使用[MaxLength]
和[StringLength]
属性时的结果。如果两者都使用,[MaxLength]
就会赢得比赛。查看下面类中studentname
列中的测试结果
public class Student
{
public Student () {}
[Key]
[Column(Order=1)]
public int StudentKey { get; set; }
//[MaxLength(50),StringLength(60)] //studentname column will be nvarchar(50)
//[StringLength(60)] //studentname column will be nvarchar(60)
[MaxLength(50)] //studentname column will be nvarchar(50)
public string StudentName { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
}
发布于 2014-11-21 06:55:34
从验证的角度来看,一切都很好,我还注意到MaxLength只在服务器端得到验证,而StringLength也在客户端得到验证。
发布于 2014-07-31 16:13:14
MaxLengthAttribute的意思是Max允许的数组或字符串数据长度
StringLengthAttribute的意思是最小。和最大。数据字段中允许的字符长度
访问http://joeylicc.wordpress.com/2013/06/20/asp-net-mvc-model-validation-using-data-annotations/
https://stackoverflow.com/questions/5717033
复制相似问题