所以,我一直在创建登录和注册方法,它使用IdentityDbContext,它采用继承IdentityUser的模型。现在我的Signup模型没有任何数据库属性或注解它基本上是一个ViewModel类。下面是模型类的代码。
public class Signup
{
[Required(ErrorMessage = "Please enter your email address!")]
[EmailAddress(ErrorMessage = "Please enter a valid registered email address")]
public string Email { get; set; }
[Required(ErrorMessage = "Please enter your first name!")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please enter your last name!")]
public string LastName { get; set; }
[Required(ErrorMessage = "Please create or enter your password")]
[Compare("ConfirmPassword", ErrorMessage = "Your password did not match. Please try again!")]
[Display(Name = "Password")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required(ErrorMessage = "Please confirm your password")]
[Display(Name = "Confirm Password")]
[DataType(DataType.Password)]
public string ConfirmPassword { get; set; }
[Required(ErrorMessage = "Please enter your date of birth")]
[DataType(DataType.Date)]
public DateTime DateOfBirth { get; set; }
[Required(ErrorMessage = "Please enter your mobile number!")]
[Display(Name = "Mobile Number")]
[DataType(DataType.PhoneNumber)]
public string PhoneNo { get; set; }
[Required(ErrorMessage = "Please enter your home address")]
[Display(Name = "Home Address")]
public string Address { get; set; }
}
对于HTML或视图,下面是我链接模型类的代码。
@model ChoreBear_Website.Models.ViewModels.Signup;
@{
Layout = null;
}
<form asp-action="Signup" class="login-inputs" asp-validation-summary="ModelOnly">
<div>
<input asp-for="Email" type="text" class="form-control email" name="email" placeholder="Email Address" required autofocus="true">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div>
<input asp-for="Password" type="password" class="form-control password" name="password" placeholder="Password" required autofocus="true">
<span class="text-danger" asp-validation-for="Password"></span>
</div>
<div>
<input asp-for="ConfirmPassword" type="password" class="form-control password" name="password" placeholder="Confirm Password" required autofocus="true">
<span class="text-danger" asp-validation-for="ConfirmPassword"></span>
</div>
<div>
<input asp-for="FirstName" type="name" class="form-control email" name="name" placeholder="John" required autofocus="true">
<span class="text-danger" asp-validation-for="FirstName"></span>
</div>
<div>
<input asp-for="LastName" type="name" class="form-control email" name="name" placeholder="Lisbon" required autofocus="true">
<span class="text-danger" asp-validation-for="LastName"></span>
</div>
<div>
<input asp-for="DateOfBirth" type="date" class="form-control email" name="dateofbirth" placeholder="1/1/1997" required autofocus="true">
<span class="text-center" asp-validation-for="DateOfBirth"></span>
</div>
<div>
<input asp-for="PhoneNo" type="phone" class="form-control email" name="phone_number" placeholder="+60172845568" required autofocus="true">
<span class="text-center" asp-validation-for="PhoneNo"></span>
</div>
<div>
<input asp-for="Address" type="text" class="form-control email" id="homeAddress" placeholder="5, South Ealing, London 65PT89, United Kingdom">
<span class="text-center" asp-validation-for="Address"></span>
</div>
<button type="submit" class="btn btn-lg btn-primary btn-block" value="Signup">Signup</button>
</form>
最后,这里是我检查ModelState.IsValid()是否。它在控制器类
中[Route("signup")]
[HttpPost]
public async Task<IActionResult> Signup(Signup customerSignup)
{
if(ModelState.IsValid)
{
//Pass all the values which are arranged and store as "Customer" model to database.
var registerUser = await _accountRepository.CreateCustomerAccountAsync(customerSignup);
ModelState.Clear();
//Check if the validation during registering the user has failed
if(!registerUser.Succeeded)
{
foreach (var error in registerUser.Errors)
{
ModelState.AddModelError("", error.Description);
}
}
}
return View();
}
现在,我运行项目,每次我在输入框中输入值(它们不是null)时,验证就会启动并禁止我创建新用户。每次我填写与模型类挂钩的所有单个输入时,验证就会在"模型"时启动。不是空的。请告知,谢谢!
PS:我在VS Code编程,所以我不知道如何调试像在Visual Studio。
我用一个简单的。net core MVC项目进行了测试。看来你把"名字"弄得一团糟。和";type"属性值。name"的值属性应该与注册模型的相应属性匹配。有问题的字段是ConfirmPassword、FirstName、LastName、PhoneNo和homeAddress。
还有一些"类型"属性无效(type="name"). 例如,它应该是type="text"对于那个领域。类似地,对于PhoneNo字段,它应该是type="tel"我还添加了一个用于电话无验证的模式。模式是"[+]{1}[0-9]{11,14}"如CSHTML代码所示。
修复这些问题后,我能够在ModelState中看到true。IsValid属性。
这是你的视图的最终编辑版本:
<div>
<input asp-for="Email" type="text" class="form-control email" name="email" placeholder="Email Address" required autofocus="true">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div>
<input asp-for="Password" type="password" class="form-control password" name="password" placeholder="Password" required autofocus="true">
<span class="text-danger" asp-validation-for="Password"></span>
</div>
<div>
<input asp-for="ConfirmPassword" type="password" class="form-control password" name="ConfirmPassword" placeholder="Confirm Password" required autofocus="true">
<span class="text-danger" asp-validation-for="ConfirmPassword"></span>
</div>
<div>
<input asp-for="FirstName" type="text" class="form-control FirstName" name="FirstName" placeholder="John" required autofocus="true">
<span class="text-danger" asp-validation-for="FirstName"></span>
</div>
<div>
<input asp-for="LastName" type="text" class="form-control LastName" name="LastName" placeholder="Lisbon" required autofocus="true">
<span class="text-danger" asp-validation-for="LastName"></span>
</div>
<div>
<input asp-for="DateOfBirth" type="date" class="form-control dateofbirth" name="dateofbirth" placeholder="1/1/1997" required autofocus="true">
<span class="text-center" asp-validation-for="DateOfBirth"></span>
</div>
<div>
<input asp-for="PhoneNo" type="tel" class="form-control PhoneNo" name="PhoneNo" pattern="[+]{1}[0-9]{11,14}" placeholder="+60172845568" required autofocus="true">
<span class="text-center" asp-validation-for="PhoneNo"></span>
</div>
<div>
<input asp-for="Address" type="text" class="form-control homeAddress" id="Address" placeholder="5, South Ealing, London 65PT89, United Kingdom">
<span class="text-center" asp-validation-for="Address"></span>
</div>
您需要删除属性上的[Required]注释,您想要接受null并添加'?如果属性与注册模型中的字符串不同,则在类型后面加上',以便ModelState验证允许您将字段留空