【发布时间】:2020-05-10 06:26:14
【问题描述】:
我有一个与验证属性相关的问题!我使用 .net 核心 2.1。我检查了 DbContext 类的 OnModelCreating 方法中的唯一字段,它工作正常。现在,如果用户在 Display & Required & MaxLength & ... 属性等输入字段中输入相同的 BirthCertificate 值(数据库中已经存在),我想显示一条错误消息,并将其发送(绑定)到 ModelState 进行检查.我还在客户端中使用 jquery.validate.js 并显示所有错误,它工作正常。我应该怎么做:
Public Class Person
{
[Display(Name = "Enter BirthCertificate")]
[Required(ErrorMessage = "Please enter {0}")]
[MaxLength(10, ErrorMessage = "Max lenght is {0}")]
public string BirthCertificate { get; set; }
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<DomainClasses.Person.Person>(entity =>
{
entity.HasIndex(e => e.BirthCertificate).IsUnique(); // it's working fine
});
}
谢谢
【问题讨论】:
标签: validation asp.net-core .net-core fluentvalidation