How to customize the default error message "The value '' is invalid" for dynamically generated form

fokwabest

New member
Joined
Aug 18, 2023
Messages
4
Programming Experience
5-10
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" when I submit the form.

Here is my View Model:

C#:
[RegularExpression("^[0-9]*$", ErrorMessage = "The value must be numeric")]
public List<int> Units_Service { get; set; }

Here is my form code:

C#:
for (int i = 0; i < Model.Workload_Category_Name.Count; i++)

{

<div class="row">

<div class="col-md-3">

           <b><u>  @Html.DisplayFor(model => model.Workload_Category_Name[i])</u> </b>

       </div>

<div class="col-md-4">

@Html.TextBoxFor(model => model.Units_Service[i], new { style = "width: 15%", MaskedTextBox = "9999" })

          @Html.ValidationMessageFor(model => model.Units_Service[i])

       </div>

   </div>

 }

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 ?
 
Just looking at that, it seems wrong to be putting in a validator saying that a List<> must match a regular expression. I can see it for a single int, but not for a List<int>.
 
Back
Top Bottom