How to customize validation error message given by text-danger?(如何自定义 text-danger 给出的验证错误消息?)
问题描述
我的 MVC .NET Core 项目的一个页面上有一个下拉列表,我想为其自定义默认验证文本.
I have a dropdown list on one of my pages in my MVC .NET Core project that I want to customise the default validation text for.
<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" />
给出的标准验证错误消息是产品 ID 字段是必需的".我想把它改成别的东西.
The standard validation error message given is "The Product ID field is required". I want to change this to something else.
我试过用这个
<span asp-validation-for="ProductID" class="text-danger">Please enter a product</span>
但是当页面加载而不是单击按钮时会显示错误消息
But the error message displays when the page loads instead of when the button is clicked
自定义验证文本的适当方法是什么?
What is the appropriate way to customise validation text?
推荐答案
一般是在你想要返回Controller的ViewModel中完成:
That's normally done in the ViewModel you want to return to the Controller:
public class SomeViewModel
{
[Required(ErrorMessage = "Your elegant error message goes here")]
public int ProductId { get; set; }
}
这篇关于如何自定义 text-danger 给出的验证错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何自定义 text-danger 给出的验证错误消息?
基础教程推荐
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
