I am new to asp.net core. I have a form which is generated dynamically. I want the validation error message to be "The value must be numeric" instead I get
"The value 'a' is invalid."
Here is my View Model:
[RegularExpression("^[0-9]*$", ErrorMessage = "The value must be numeric")]
public List Units_Service { get; set; }
Here is my form code:
for (int i = 0; i < Model.Workload_Category_Name.Count; i++)
{
@Html.DisplayFor(model => model.Workload_Category_Name[i])
@Html.TextBoxFor(model => model.Units_Service[i], new { style = "width: 15%", MaskedTextBox = "9999" })
@Html.ValidationMessageFor(model => model.Units_Service[i])
}
Despite the fact, I have put the custom error message in my View Model as shown above, I keep getting the default message "The value '' is invalid". Please what is the solution to this kind of scenario ?
Mahesh ChandPosted Nov 1, 2023, 3:09 PM
You need to customize the error message somewhere. Something like this I got from ChatGPT :)
In the Model:
You can use data annotations to customize the error messages on properties.
In the Controller:
You can customize the error response based on the model state.
In the View:
Display the error message using the validation summary or display it next to the field.