如何自定义text-danger给出的验证错误消息?

cra*_*Pen 5 c# validation asp.net-core-mvc

我在我的MVC .NET Core项目中的一个页面上有一个下拉列表,我想自定义默认验证文本.

<select asp-for="ProductID" class="form-control" asp-items="ViewBag.ProductID">
    <option value="">--Select Product --</option>
</select>
<span asp-validation-for="ProductID" class="text-danger" />
Run Code Online (Sandbox Code Playgroud)

给出的标准验证错误消息是"产品ID字段是必需的".我想将此更改为其他内容.

我试过用这个

 <span asp-validation-for="ProductID" class="text-danger">Please enter a product</span>
Run Code Online (Sandbox Code Playgroud)

但是,页面加载时会显示错误消息,而不是单击按钮时

自定义验证文本的适当方法是什么?

SO *_*ood 8

这通常是在想要返回Controller的ViewModel中完成的:

public class SomeViewModel
{
    [Required(ErrorMessage = "Your elegant error message goes here")]
    public int ProductId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)