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
{
}
}
本文介绍了一个使用 C# 的数据注解进行模型验证的例子,包括学生和注册信息的属性验证,如姓名长度限制和成绩范围等。
706

被折叠的 条评论
为什么被折叠?



