Nguon: Microsoft Learn · .NET 8.0

Validation Summary Tag Helper trong ASP.NET Core

Nguồn: Tag Helpers in forms - The Validation Summary Tag Helper

Validation Summary Tag Helper

Validation Summary Tag Helper (Tag Helper Tóm tắt Xác thực) được sử dụng để hiển thị tóm tắt các thông báo xác thực. Giá trị thuộc tính asp-validation-summary có thể là một trong các giá trị sau:

asp-validation-summaryThông báo xác thực được hiển thị
AllCấp property và cấp model
ModelOnlyChỉ cấp model
NoneKhông có

Ví dụ

Trong ví dụ sau, data model (mô hình dữ liệu) có các thuộc tính DataAnnotation tạo ra các thông báo lỗi xác thực trên phần tử <input>. Khi xảy ra lỗi xác thực, Validation Tag Helper hiển thị thông báo lỗi:

csharp
using System.ComponentModel.DataAnnotations;

namespace FormsTagHelper.ViewModels
{
    public class RegisterViewModel
    {
        [Required]
        [EmailAddress]
        [Display(Name = "Email Address")]
        public string Email { get; set; }

        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    }
}
cshtml
@model RegisterViewModel

<form asp-controller="Demo" asp-action="RegisterValidation" method="post">
    <div asp-validation-summary="ModelOnly"></div>
    <label>Email: <input asp-for="Email" /></label> <br />
    <span asp-validation-for="Email"></span><br />
    <label>Password: <input asp-for="Password" /></label><br />
    <span asp-validation-for="Password"></span><br />
    <button type="submit">Register</button>
</form>

HTML được tạo ra (khi model hợp lệ):

html
<form action="/DemoReg/Register" method="post">
  <label>Email: <input name="Email" id="Email" type="email" value=""
   data-val-required="The Email field is required."
   data-val-email="The Email field is not a valid email address."
   data-val="true"></label><br>
  <span class="field-validation-valid" data-valmsg-replace="true"
   data-valmsg-for="Email"></span><br>
  <label>Password: <input name="Password" id="Password" type="password"
   data-val-required="The Password field is required." data-val="true"></label><br>
  <span class="field-validation-valid" data-valmsg-replace="true"
   data-valmsg-for="Password"></span><br>
  <button type="submit">Register</button>
  <input name="__RequestVerificationToken" type="hidden" value="<removed for brevity>">
</form>

Cách hoạt động của Validation Summary Tag Helper

Khi asp-validation-summary="ModelOnly" được sử dụng, chỉ các lỗi xác thực cấp model (không phải lỗi cấp property) mới được hiển thị trong phần tử <div>. Điều này hữu ích khi bạn muốn hiển thị các lỗi cụ thể cho từng trường bên cạnh trường đó (sử dụng Validation Message Tag Helper) và chỉ tóm tắt các lỗi toàn cục model ở đầu form (biểu mẫu).

Khi asp-validation-summary="All" được sử dụng, cả lỗi cấp model và cấp property đều được hiển thị trong phần tử <div>. Đây thường là lựa chọn phù hợp cho các form đơn giản không sử dụng Validation Message Tag Helper cho từng trường riêng lẻ.

Khi asp-validation-summary="None" được sử dụng, phần tử <div> sẽ không hiển thị bất kỳ thông báo xác thực nào. Điều này hữu ích nếu bạn muốn kiểm soát hoàn toàn việc hiển thị lỗi xác thực trong view.