asp.net MVC 添加验证最优方式

来源:互联网 发布:威廉 切尔西 知乎 编辑:程序博客网 时间:2024/04/29 19:40
using System;using System.ComponentModel.DataAnnotations;namespace ContosoSite.Models{    public class StudentMetadata    {        [StringLength(50)]        [Display(Name="Last Name")]        public string LastName;        [StringLength(50)]        [Display(Name="First Name")]        public string FirstName;        [StringLength(50)]        [Display(Name="Middle Name")]        public string MiddleName;        [Display(Name = "Enrollment Date")]        public Nullable<System.DateTime> EnrollmentDate;    }    public class EnrollmentMetadata    {        [Range(0, 4)]        public Nullable<decimal> Grade;    }}

然后:

using System;using System.ComponentModel.DataAnnotations;namespace ContosoSite.Models{    [MetadataType(typeof(StudentMetadata))]    public partial class Student    {    }    [MetadataType(typeof(EnrollmentMetadata))]    public partial class Enrollment    {    }}


0 0