“我正在使用ASP.NET Core 6应用程序,需要在警告框中显示验证摘要数据。我想在用户友好的警告框中显示任何验证错误或摘要信息。如何在ASP.NET Core 6中实现这一点?”
这是我的代码…
<div class="container">
<form class="w-100" asp-controller="Home" asp-action="Index" method="post">
<div asp-validation-summary="All" ></div>
<div class="d-flex">
<div>
<label class="form-label" asp-for="Name">Username</label>
<input asp-for="Name" class="form-control" class="w-100" />
@* <small><span asp-validation-for="Name"></span></small> *@
</div>
<div>
<label class="form-label" asp-for="password">Password</label>
<input asp-for="password" class="form-control" class="w-100" />
@* <small><span asp-validation-for="password"></span></small> *@
</div>
<div>
<label class="form-label" asp-for="ConfirmPassword">Confirm Password</label>
<input asp-for="ConfirmPassword" class="form-control" class="w-100" />
@* <small><span asp-validation-for="ConfirmPassword"></span></small> *@
</div>
</div>
<div class="d-flex">
<div>
<label class="form-label" asp-for="Age">Age</label>
<input asp-for="Age" class="form-control" class="w-100" />
@* <small><span asp-validation-for="Age"></span></small> *@
</div>
<div>
<label class="form-label" asp-for="Batch">Batch</label>
<input asp-for="Batch" class="form-control" class="w-100"/>
@* <small><span asp-validation-for="Batch"></span></small> *@
</div>
</div>
<div class="d-flex">
<div>
<label class="form-label" asp-for="Email">E-mail</label>
<input asp-for="Email" class="form-control" class="w-100"/>
@* <small><span asp-validation-for="Email"></span></small> *@
</div>
<div>
<label class="form-label" asp-for="YourWebsite">You website name</label>
<input asp-for="YourWebsite" class="form-control" class="w-100"/>
@* <small><span asp-validation-for="YourWebsite"></span></small> *@
</div>
<div>
<label class="form-label" asp-for="Phone">Enter mobile no.</label>
<input asp-for="Phone" class="form-control" class="w-100"/>
@* <small><span asp-validation-for="Phone"></span></small> *@
</div>
</div>
<div>
<label class="form-label" asp-for="yourAddress"></label>
<input asp-for="yourAddress" class="form-control" class="w-100" />
@* <small><span asp-validation-for="yourAddress"></span></small> *@
</div>
<br />
<br />
<div>
<button class="btn btn-danger text-white">submit now !</button>
</div>
</form>
</div>
字符串
这是我的模特课
public class CandidateModel
{
[Required(ErrorMessage ="field should be filled !")]
public string Name { get; set; }
[Required(ErrorMessage = "field should be filled !")]
[Range(minimum:18, maximum:35,ErrorMessage ="Applicable for age 18 to 35")]
public int? Age { get; set; }
[Required(ErrorMessage = "field should be filled !")]
[Range(minimum:2015, maximum:2023, ErrorMessage ="only for batch 2015 to 2023")]
public string Batch { get; set; }
[Required(ErrorMessage = "field should be filled !")]
[RegularExpression("^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$",ErrorMessage ="invalid e-mail")]
public string Email { get; set; }
[Required(ErrorMessage = "field should be filled !")]
[RegularExpression("(?=^.{8,}$)((?=.*\\d)|(?=.*\\W+))(?![.\\n])(?=.*[A-Z])(?=.*[a-z]).*$", ErrorMessage = "write atleast : one-digit, one-small character, one-capital letter, one-special character and 8-character long ")]
public string password { get; set; }
[Required(ErrorMessage = "field should be filled !")]
[Compare("password",ErrorMessage ="please confirm your password")]
public string ConfirmPassword { get; set; }
[Required(ErrorMessage = "field should be filled !")]
[Url(ErrorMessage ="enter correct url please !")]
public string YourWebsite { get; set; }
[Required(ErrorMessage = "field should be filled !")]
[MaxLength(10,ErrorMessage ="only 10 number allowed")]
[MinLength(10,ErrorMessage = "only 10 number allowed")]
public string Phone { get; set; }
[Display(Name="your Address please")]
[Required(ErrorMessage = "field should be filled !")]
public string yourAddress { get; set; }
}
}
型
这是我的控制器
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(CandidateModel cmd)
{
if (ModelState.IsValid)
{
ModelState.Clear();
}
return View();
}
型
我希望验证错误消息显示在警报框中。
1条答案
按热度按时间lbsnaicq1#
根据您的描述,我建议您可以使用swal来实现您的要求。
您可以添加以下脚本并修改验证摘要div,如下所示:
:
字符串
“查看”页修改验证摘要div:
型
的数据