我有一个 Web 表单,用于捕获用户的一些详细信息以发送到后端。注册表单有一个名为的模型类RegisterModel
,用于验证用户在该表单中输入的所有输入。当我输入满足所需条件的输入时,模型状态不会对其他字段抛出错误,但它会为确认密码字段抛出错误,即使该字段不为空并与之前的密码输入匹配。
RegisterModel
:
[Required(ErrorMessage ="The password field is required")]
[MinLength(8, ErrorMessage ="The password needs to be at least eight characters long")]
[MaxLength(50, ErrorMessage ="The password length cannot exceed fifty characters")]
[RegularExpression("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\W).+$", ErrorMessage = "Password must contain at least one uppercase letter, one lowercase letter, and one special character.")]
public string Password { get; set; }
[Required(ErrorMessage = "You have to confirm the password")]
[Compare("Password", ErrorMessage = "The passwords do not match")]
public string Confirmpassword { get; set; }
注册表单的代码数据以及所有必需的控制器和操作名称属性
註冊表:
@model Christian_Social.Models.RegisterModel
<div class="container">
<div class="text-center">
<h4 style="color:blue">Create a new account to proceed</h4>
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card mt-5">
<div class="card-body">
<form asp-controller="Register" asp-action="CreateEntry" method="post">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username"asp-for=username required>
<span class="text-danger" style="color:red" asp-validation-for="username"></span>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" asp-for="Email" required>
<span class="text-danger" style="color:red" asp-validation-for="Email"></span>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" asp-for="Password"required>
<span class="text-danger" style="color:red" asp-validation-for="Password"></span>
</div>
<div class="mb-3">
<label for="confirm-password" class="form-label">Confirm Password</label>
<input type="password" class="form-control" id="confirm-password" name="confirm-password" asp-for="Confirmpassword" required>
<span class="text-danger" style="color:red" asp-validation-for="Confirmpassword"></span>
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
以下是提交表单时我处理控制器逻辑的方式:
RegisterController
:
public class RegisterController : Controller
{
private readonly DatabaseContext databaseContext;
public RegisterController(DatabaseContext dataContext)
{
databaseContext = dataContext;
}
public IActionResult CreateEntry(RegisterModel registerModel)
{
if (ModelState.IsValid)
{
// return the account created successfully page
// encrypt the password, since passwords are private
var encryptedPassword = BCrypt.Net.BCrypt.HashPassword(registerModel.Password);
var userName = registerModel.username;
var email = registerModel.Email;
// create a new database entity
var entity = new User
{
Username = userName,
Email = email,
Password = encryptedPassword,
};
// make sure you are not adding duplicate entries
var user = databaseContext.Users.Where(p=>p.Email == email).FirstOrDefault();
if (user == null)
{
// entry does not exist, add a new one
databaseContext.Add(entity);
// save changes
databaseContext.SaveChanges();
return View("~Views/Home/Index");
}
else
{
// add errors to the model state and return the same view
ModelState.AddModelError("Email", "That email is already taken");
return View("~Views/Home/Register", registerModel);
}
}
else
{
return View("~/Views/Home/Register.cshtml", registerModel);
}
}
}
当字段中有密码并且与密码字段中的输入匹配时,为什么我的表单会返回错误?
表单字段的名称应与 RegisterModel 中的属性名称匹配。具体来说,确认密码字段的 name 属性应与模型属性名称匹配。
此外,它还需要在视图中包含客户端验证脚本。如果没有添加,我们需要添加这些
还请检查模型中属性名称的大小写是否与表单字段名称匹配。