c# - 客户的确认密码在他的 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();}
解决方案
推荐阅读
- mysql - PDOException::("PDO::__construct(): 使用 MySQL 8 / PHP 7.2 / Laravel 执行缓存_sha2 auth 时出现意外的服务器响应:109")
- r - 使用 lookupUsers 获取 twitter 句柄列表时出现 HTTP 403 错误
- java - Xodus 需要 txn.commit() 吗?
- data-modeling - 尺寸建模中的桥表
- php - 在 appengine 上使用 php 将大文件下载到云存储
- python - Matplotlib xticklabels 显示年份而不是月份
- c - 错误:非法指令。Mips 交叉编译器
- python - 根据条件比较熊猫数据框行
- neural-network - 如何在 Keras 中为 Sequential 模型实现损失函数(Intersection over Union)?
- php - SQL SELECT 返回数组,但 PHP 认为它为空