Hi, everyone. I am working on a c# mvc application. I validate a string property as Required in a ViewModel. Here is my code from the ViewModel:
[Required (ErrorMessage = "Please enter a number.")]
[RegularExpression("^[0-9]*$", ErrorMessage = "Please enter only a number not another sign.")]
public string BoardNumber { get; set; }
Here is the code from the View:
<table style="background-color:rgba(224, 95, 3, 0.18);border-radius:8px;" align="center">
<tr>
<td>
"margin-left: 20px">BoardNumber:
<p style="margin-left: 20px; margin-right:20px;">
@Html.TextBoxFor(x => x.BoardNumber, new { @class = "form-control" , @Value = "" }) <br />
@Html.ValidationMessageFor(x => x.BoardNumber, "", new { @class = "bg-warning" })
@Html.ValidationMessage("error", "", new { @class = "bg-warning" })
</p>
</td>
<td>
<p>
<input type="submit" style="margin-left: 20px; margin-right:20px; margin-top: 10px;" value="Submit" class="btn btn-primary orders-search-button">
</p>
</td>
</tr>
</table>
The problem is that when I go to the view, the required error message is shown even though I haven't pressed submit, and I want it to be displayed only if I press Submit and haven't entered anything. How should I fix that?
What I have tried:
I haven't tried anything because I don't know what to do.