将asp-validation-for连接到新错误的方法如下:
<input type="text" id="email" name="email" asp-validation-for="email" />
<span asp-validation-summary="ModelOnly" class="text-danger"></span>
public class YourModel
{
[Required(ErrorMessage = "Email is required")]
[EmailAddress(ErrorMessage = "Invalid email address")]
public string Email { get; set; }
}
[HttpPost]
public IActionResult YourAction(YourModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
// 处理表单提交的逻辑
return RedirectToAction("Success");
}
<span asp-validation-for="email" class="text-danger"></span>
这样,当用户提交表单时,如果输入字段的值不符合验证规则,将会显示相应的错误信息。
推荐的腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云