首页 > 解决方案 > 客户的确认密码在他的 Asp.Net Core 帐户中出现 ModelState 错误

问题描述

我有一个包含客户的帐户类和一个包含帐户的客户类。我创建了一个名为 Client_Account 的连接实体。问题是,每当我尝试编辑帐户时,都会收到有关客户端确认密码的模型状态错误。这是在 asp.net 核心 2.2 中。具体来说,错误发生在控制器方法中的 await TryUpdateModelAsync 行。ModelState 对键“Client_Accounts[0].Client.ConfirmPassword”和值“密码和确认密码不匹配”无效。我尝试使用 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,}$",
       ErrorMessage = "Must have at least 8 characters one upper-case, one lower-case, 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();}

标签: c#asp.net-coremodelstate

解决方案


推荐阅读