using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace WebCoreDemo.Models
{
/// <summary>
/// 学生模型
/// </summary>
public class Student
{
// Display 显示
/// <summary>
/// 学号
/// </summary>
public int Id { get; set; }
/// <summary>
/// 姓名
/// </summary>
[Display(Name = "姓名")]
[Required(ErrorMessage = "请输入名字"),MaxLength(50,ErrorMessage = "名字的长度不能超过50个字符")]
public string Name { get; set; }
/// <summary>
/// 班级 ?为可空
/// </summary>
[Display(Name = "班级信息")]
[Required(ErrorMessage = "请选择班级信息")]
public ClassNameEnum? ClassName { get; set; }
/// <summary>
/// 邮箱
/// </summary>
[Display(Name = "邮箱地址")]
[Required(ErrorMessage = "请输入邮箱地址")]
[RegularExpression(@"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$",
ErrorMessage = "邮箱的格式不正确")]
public string Email { get; set; }
/// <summary>
/// 图片地址
/// </summary>
[Display(Name = "图片地址")]
public string PhotoPath { get; set; }
}
/*** 模型验证
* Required 指定该字段是必填的
* Range 指定允许的最小值和最大值
* MinLength 只用MinLength指定字符串的最小长度
* MaxLength 使用MaxLength指定字符串的最大长度
* Compare 比较模型的2个属性。例如比较Email和ComfirmEmail属性
* RegularExpression 正则表达式 验证提供的值是否与正则表达式指定的模式匹配
*/
/// <summary>
/// 血糖视图模型
/// </summary>
public class SugarModel
{
/// <summary>
/// 用户Id
/// </summary>
[Required]
public Guid Id { get; set; }
/// <summary>
/// 血糖值
/// </summary>
[DisplayName("血糖值"), Required(ErrorMessage = "请填写{0}")]
public double XueTang { get; set; }
/// <summary>
/// 日期(年月日)
/// </summary>
[DisplayName("采集日期"), Required(ErrorMessage = "请选择{0}")]
public DateTime GatherDate { get; set; }
}
/// <summary>
/// 登录视图模型
/// </summary>
public class LoginModel
{
/// <summary>
/// 用户名
/// </summary>
[DisplayName("用户名"), Display(Name = "用户名"), Required(ErrorMessage = "请填写{0}"), StringLength(12, MinimumLength = 5, ErrorMessage = "{0}长度为{1}至{2}个字符")]
public string UserName { get; set; }
/// <summary>
/// 密码
/// </summary>
[DisplayName("密码"), Display(Name = "密码"), Required(ErrorMessage = "请填写{0}"), StringLength(24, MinimumLength = 8, ErrorMessage = "{0}长度为{1}至{2}个字符")]
public string Password { get; set; }
}
/// <summary>
/// 病历信息模型
/// </summary>
public class MedicalModel
{
/// <summary>
/// 用户Id
/// </summary>
[Required]
public Guid Id { get; set; }
/// <summary>
/// 主诉
/// </summary>
[DisplayName("主诉"), Required(ErrorMessage = "{0}不能为空"), StringLength(24, ErrorMessage = "{0}最多包含{2}个字符")]
public string ZhuSu { get; set; }
/// <summary>
/// 检查
/// </summary>
[StringLength(36)]
public string JianCha { get; set; }
/// <summary>
/// 诊断
/// </summary>
[StringLength(36)]
public string ZhenDuan { get; set; }
/// <summary>
/// 处理
/// </summary>
[StringLength(52)]
public string ChuLi { get; set; }
/// <summary>
/// 诊断日期
/// </summary>
[DisplayName("诊断日期"), Required(ErrorMessage = "请选择{0}")]
public DateTime DiagnoseDate { get; set; }
}
[ProductRule]
public class Product : IValidatableObject
{
public int ID { get; set; }
[Display(Name = "产品编号")]
[Required(ErrorMessage = "{0}是必填的")]
public string ProductCode { get; set; }
[Display(Name = "产品名称")]
[StringLength(50, MinimumLength = 5, ErrorMessage = "{0}的长度应在{2}~{1}之间")]
[Required(ErrorMessage = "{0}是必填的")]
public string ProductName { get; set; }
[Display(Name = "单价")]
public decimal Amount { get; set; }
[Display(Name = "产品数量")]
[Required(ErrorMessage = "{0}是必填的")]
public int Qty { get; set; }
[Display(Name = "所有者邮箱")]
[EmailAddress(ErrorMessage = "{0}格式不正确")]
public string OwerEmail { get; set; }
[Display(Name = "备注")]
[MaxLength(200, ErrorMessage = "{0}最大长度为200")]
public string Remark { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (!Remark.Contains(ProductName))
{
yield return new ValidationResult(errorMessage: "备注中没有包含产品名称",
new[] { nameof(Remark) });
//new[] { nameof(Remark) }这个参数是一个数组,它表示这个错误是哪一个字段引起的,
//如果涉及到多个字段,那么应该归为这个类的错误,即:new[] { nameof(Product) }
}
}
}
/// <summary>
/// 班级的枚举
/// </summary>
public enum ClassNameEnum
{
[Display(Name = "未分配")]
None,
[Display(Name = "一年级")]
FirstGrade,
[Display(Name = "二年级")]
SecondGrade,
[Display(Name = "三年级")]
GradeThree
}
}
ASP.NET Core MVC 模型验证 示例
最新推荐文章于 2024-05-02 13:16:25 发布