客户的确认密码在Asp.Net Core中的帐户中出现ModelState错误

问题描述

我有一个包含客户的Account类和一个包含客户的Client类。我创建了一个名为Client_Account的加入实体。问题是,每当我尝试编辑帐户时,都会收到有关客户确认密码的modelstate错误。这在asp.net core 2.2中。具体来说,该错误发生在控制器方法中的tryTryUpdateModelAsync行。通过密钥“ Client_Accounts [0] .Client.ConfirmPassword”和值“密码和确认密码不匹配”,ModelState无效。我试过使用ModelState.Remove和ModelState.ClearValidationState,但没有结果。

这是类和帐户控制器的编辑方法

   public class Client
   {
       [Key]
       public int Client_ID { get; set; }

       [required]
       [display(Name = "Name")]
       public string Client_Name { get; set; }

       [required]
       [display(Name = "Username")]
       public string Client_Username { get; set; }
       
       [required(ErrorMessage = "Password is required")]
       [RegularExpression(@"(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,}$",ErrorMessage = "Must have at least 8 characters one upper-case,one lower-case,a digit and a non 
       alphanumeric character")]
       [display(Name = "Password")]
       [DataType(DataType.Password)]
       public string Client_Password { get; set; }

       [required]
       [RegularExpression(@"(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,a digit and a non 
       alphanumeric character")]
       [DataType(DataType.Password)]
       [Compare("Client_Password",ErrorMessage = "The password and confirmation password do not 
       match.")]
       [display(Name = "Confirm Password")]
       [NotMapped]
       public string ConfirmPassword { get; set; }

       [required]
       [display(Name = "Address")]
       public string Client_Address { get; set; }
       [required]
       [display(Name = "Telephone")]
       [RegularExpression(@"^\(?([0-9]{10})$",ErrorMessage = "Not a valid Phone number")]
       public string Client_Tel { get; set; }
      
       [required]
       [EmailAddress]
       [RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}",ErrorMessage = "Incorrect Email 
       Format")]
       [display(Name = "Email")]
       public string Client_Email { get; set; }

       [display(Name = "Accounts")]
       public List<Client_Account> Client_Accounts { get; set; }
    }
    public class Account
   {
       [Key]
       public int Account_ID { get; set; }
       [required]
       [display(Name = "IBAN")]
       public string Account_IBAN { get; set; }

       [required]
       [display(Name = "Balance")]
       [DataType(DataType.Currency)]
       [displayFormat(DataFormatString = "{0:N2}")]
       public decimal Account_Balance { get; set; }

       [display(Name = "Account Holders")]
       public List<Client_Account> Client_Accounts { get; set; }
   }
    public class Client_Account
    {
        public int Client_ID { get; set; }
        public Client Client { get; set; }

        public int Account_ID { get; set; }
        public Account Account { get; set; }
   }
 public async Task<IActionResult> Edit(int id,string[] selectedClients)
        {
            if (id == null)
            {
                return NotFound();
            }
            var account = await _context.Account
            .Include(c => c.Client_Accounts)
            .ThenInclude(c => c.Client) 
            .FirstOrDefaultAsync(m => m.Account_ID == id);

            
            if (await TryUpdateModelAsync<Account>(account,"",a => a.Account_IBAN,a => a.Account_Balance))
            {      await _context.SaveChangesAsync();}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)