ASP.NET Core 6 MVC
我在显示模型的验证消息时遇到问题。
在下面的代码中, asp-validation-summary="All" 正确显示所有消息,而 asp-validation-summary="ModelOnly" 不显示任何内容。我在呈现的 HTML 代码中注意到的一个问题是 span class="field-validation-valid" 的类名,而它应该是 class="field-validation-error"。我不明白为什么,以前有人遇到过这个问题吗?
<div asp-validation-summary="All" class="text-danger"></div>
<div class="card col-xxl-6 col-xl-6 col-lg-6 col-md-6 col-sm-12 col-xs-12 offset-xxl-3 offset-xl-3 offset-lg-3 offset-md-3">
<div class="card-header">
<p class="card-title fs-4">Veuillez saisir les données du compte</p>
</div>
<div class="card-body">
<form method="post" asp-controller="user" asp-action="create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="row align-items-center mb-3">
<div class="input-group">
<span class="input-group-text">Prénom</span>
@*@Html.EditorFor(m => m.FirstName, new { htmlAttributes = new { @class = "form-control rounded" } })*@
<input asp-for="@Model.FirstName" name="model.FirstName" aria-describedby="userCreate" type="text" class="form-control rounded" />
<span asp-validation-for="@Model.FirstName" class="text-danger"></span>
</div>
</div>
呈现的 HTML
<div class="card-body">
<form method="post" action="/user/create">
<div class="row align-items-center mb-3">
<div class="input-group">
<span class="input-group-text">Prénom</span>
<input name="model.FirstName" aria-describedby="userCreate" type="text" class="form-control rounded" data-val="true" data-val-length="The field FirstName must be a string with a minimum length of 3 and a maximum length of 60." data-val-length-max="60" data-val-length-min="3" data-val-required="The FirstName field is required." id="FirstName" maxlength="60" value="">
<span class="text-danger field-validation-valid" data-valmsg-for="FirstName" data-valmsg-replace="true"></span>
</div>
</div>
<div class="row align-items-center mb-3">
<div class="input-group">
<span class="input-group-text">Nom</span>
<input name="model.LastName" aria-describedby="userCreate" type="text" class="form-control rounded" data-val="true" data-val-required="errroooor" id="LastName" value="">
<span class="text-danger field-validation-valid" data-valmsg-for="LastName" data-valmsg-replace="true"></span>
</div>
</div>
<div class="row align-items-center mb-3">
<div class="input-group">
<span class="input-group-text">Email</span>
<input name="model.Email" aria-describedby="userCreate" type="text" class="form-control rounded" data-val="true" data-val-required="The Email field is required." id="Email" value="">
<span class="text-danger field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>
</div>
</div>
<div class="row align-items-center mb-3">
<div class="input-group">
<span class="input-group-text">Mot de passe</span>
<input name="model.Password" aria-describedby="userCreate" type="text" class="form-control rounded" data-val="true" data-val-required="The Password field is required." id="Password" value="">
<span class="text-danger field-validation-valid" data-valmsg-for="Password" data-valmsg-replace="true"></span>
</div>
</div>
<div class="input-group mb-3">
<span asp-for="Role.Id" class="input-group-text">Statut du compte<span class="text-danger">*</span></span>
<select name="model.IdRole" class="form-select" data-val="true" data-val-required="The Id field is required." id="Role_Id">
<option value="0">Sélectionnez un statut</option>
<option value="1">SuperAdmin</option>
<option value="2">Admin</option>
<option value="3">Stagiaire</option>
</select>
</div>
<div class="d-flex justify-content-end">
<button type="button" class="btn btn-primary rounded me-3">Annuler</button>
<button type="submit" class="btn btn-primary rounded">Enregistrer</button>
</div>
<input name="__RequestVerificationToken" type="hidden" value="CfDJ8Jw8z2cGDsNLqIg1HmqXyL0aPMFYkBmazv4FHneympXaE5-YFo1c2TvRqRau479jGucSZKDWHnXMBcTHfq9VO8fjHDhoYwGUEDOHNVv413nXa2MSfawPr-yO6RP1lnhu_1EcKN7yoyVlW2FM_BQoyi4"></form>
</div>
public class UserViewModel
{
public int Id { get; set; }
public IEnumerable<RoleViewModel> Roles { get; set; }
public RoleViewModel? Role { get; set; }
public int IdRole { get; set; }
[Required]
public string LastName { get; set; }
[StringLength(60, MinimumLength = 3)]
[Required]
public string FirstName { get; set; }
[Required]
public string Email { get; set; }
public string? Token { get; set; }
// [Required]
// public string Login { get; set; }
[Required]
public string Password { get; set; }
public List<SessionViewModel>? Sessions { get; set; }
public UserViewModel()
{
this.Sessions = new List<SessionViewModel>();
this.Roles = new List<RoleViewModel>();
}
public UserViewModel(RoleViewModel role, string lastName, string firstName, string email, /*string login,*/ string password) : this()
{
this.Role = role;
this.IdRole = role.Id;
this.LastName = lastName;
this.FirstName = firstName;
this.Email = email;
// this.Login = login;
this.Password = password;
}
}
}