首页 > 代码库 > c# MVC模式学习笔记_数据验证

c# MVC模式学习笔记_数据验证

改变显示字段名称 设计字段规范 

1.引用

1 using System.ComponentModel;
2 using System.ComponentModel.DataAnnotations;

2.DisplayName值

 public class Book
    {
        public int BookID { get; set; }   
        [DisplayName("ISBN")]
        public string isbn { get; set; }
        [DisplayName("图书名称:")]
        [Required(ErrorMessage="图书名称必填")]
        public string bookName { get; set; }
        [DisplayName("作者")]
        [Required(ErrorMessage = "作者必填")]
        public string author { get; set; }
          [DisplayName("出版社")]
          [Required(ErrorMessage = "出版社必填")]
        public string press { get; set; }
          [DisplayName("出版日期")]
          [Required(ErrorMessage = "出版日期必填")]
        public DateTime pressDate { get; set; }
          [DisplayName("定价:")]
        public decimal price { get; set; }
          [DisplayName("尺寸:")]
        public string size { get; set; }
         [DisplayName("重量:")]
        public double? weight { get; set; }
         [DisplayName("开本:")]
        public int? format { get; set; }
    }

3.设置字段范围

[StringLength(50)]
 [StringLength(10,MinimumLength=2)]

range 指定数值型范围 

 [Range(0,10000)]

 

c# MVC模式学习笔记_数据验证