The Required attribute does not seem to work on integer values. I also tried Range attribute on integer field and is behaving normal. The issues are that the validation is not made and the required message in front-end is not showing. For string values the attribute is behaving as expected. This is the sample code:
@page "/test"
<EditForm Model="@exampleModel">
<DataAnnotationsValidator />
<ValidationSummary />
<InputNumber @bind-Value="exampleModel.Name2"></InputNumber>
<button type="submit" value="Save" >
Submit
</button>
</EditForm>
@code {
private ExampleModel exampleModel = new ExampleModel();
public partial class ExampleModel
{
[Required]
public string Name { get; set; }
[Required]
[Range(1, 100, ErrorMessage = "Error Test")]
public int Name2 { get; set; }
}
}
The Required attribute for Name is working while the one for Name2 is not. The Range attribute for Name2 is working as expected.
I'm using ASP.NET Core 3.1 with Visual Studio 16.9.4.
int(as opposed toint?) so it always going to have a value (0) and therefore required will not be flagged. Use int? if it is optional.