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

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

(不工作)

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

(加工)

[required]
[StringLength(80)]
[displayName("Contact Name:")]
public string ContactName { get; set; }

解决方法

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

根据实体框架的Microsoft官方网站[MaxLenth]属性
因为实体框架知道您的案例中数据库中列的最大长度是多少(例如varchar(80))

指定属性中允许的数组或字符串数​​据的最大长度.

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

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

但是在第二种情况下[StringLength(80)]正在工作,因为它对实体框架没有任何依赖.

如果您使用的是实体框架,或者没有它,SO [StringLength(80)]将在两种情况下工作

指定数据字段中允许的字符的最小和最大长度.

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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....