Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation 如何在asp.net core中显示唯一字段的错误_Validation_Asp.net Core_.net Core_Fluentvalidation - Fatal编程技术网

Validation 如何在asp.net core中显示唯一字段的错误

Validation 如何在asp.net core中显示唯一字段的错误,validation,asp.net-core,.net-core,fluentvalidation,Validation,Asp.net Core,.net Core,Fluentvalidation,我有一个与验证属性相关的问题!我使用.NETCore2.1。我检查了DbContext类中OnModelCreating方法中的唯一字段,它工作正常。现在,如果用户在输入字段中输入相同的出生证书值(数据库中已经存在),我想显示一条错误消息,如display&Required&MaxLength&。。。属性并将其发送(绑定)到ModelState以进行检查。我还在客户端中使用jquery.validate.js并显示所有错误,它工作正常。我应该如何做到这一点: Public Class Perso

我有一个与验证属性相关的问题!我使用.NETCore2.1。我检查了DbContext类中OnModelCreating方法中的唯一字段,它工作正常。现在,如果用户在输入字段中输入相同的出生证书值(数据库中已经存在),我想显示一条错误消息,如display&Required&MaxLength&。。。属性并将其发送(绑定)到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
    });
}
公共类人物
{
[显示(Name=“输入出生证”)]
[必需(ErrorMessage=“请输入{0}”)]
[MaxLength(10,ErrorMessage=“最大长度为{0}”)]
公共字符串出生证书{get;set;}
}
模型创建时受保护的覆盖无效(ModelBuilder)
{
builder.Entity(Entity=>
{
entity.HasIndex(e=>e.BirthCertificate).IsUnique();//工作正常
});
}

谢谢

您可以使用其中任何一种

1) 在SQL Server中为出生证书创建新的唯一密钥。使用catch(Exception ex)并返回到ajax以显示错误

2) 检查代码吗

if (db.Person.Where(x => x.BirthCertificate.Contains(birthcert)).Any())
{
     //return to ajax to show error
}