首先,在模型类中引用 System.ComponentModel.DataAnnotations 命名空间;System.ComponentModel.DataAnnotations 命名空间提供定义 ASP.NET MVC 和 ASP.NET 数据控件的类的特性(即内置验证属性 ...
首先,在模型类中引用 System.ComponentModel.DataAnnotations 命名空间;System.ComponentModel.DataAnnotations 命名空间提供定义 get='_blank'>ASP.NET MVC 和 ASP.NET 数据控件的类的特性(即内置验证属性,你可以使用声明的任何类或属性)
在没有添加属性的特性的时候我们是可以添加空数据的,比如下面的Title字段
现在我们就为Movie类的属性都加上验证特性,然后再运行
public class Movie { public int ID { get; set; } [Required]//提交的数据为非空 public string Title { get; set; } public DateTime ReleaseDate { get; set; } public string Genre { get; set; } [Range(1, 100)]//提交的数据是1至00 public decimal Price { get; set; } [StringLength(5)]//提交字符串长度为5 public string Rating { get; set; } }
原标题:ASP.NET MVC系列:添加模型的验证规则
关键词:ASP.NET
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。