I have a Blazor WebAssembly app that contains an InputBox with options.
- When the is loaded for the first time, nothing is selected and shows "Please select an option" followed by the DataAnnotation validations like [Required].
**Data Model**
class Meeting
...
[Required(ErrorMessage = "Please Select a Branch")]
public string SIteGID { get; set; }
...
**Meeting.razor**
<EditForm Model="@meetingObject">
<DataAnnotationsValidator />
...
<div class="form-group">
<label for="groupMemberBranch" class="label-control">BRANCH</label>
<InputSelect id="groupMemberBranch" @bind-Value="meetingObject.SIteGID" @onchange="OnValueChanged" class="form-control custom-select" title="The selection of a Branch Required ">
<option value="Select" selected disabled="disabled">(Please select a Branch)</option>
@foreach (Branch branch in branchResponseResult.Data.ToList())
{
<option value="@branch.SiteGID"> @branch.BranchDescription</option>
}
</InputSelect>
<ValidationMessage For="@(()=> meetingDTO.SIteGID)" />
</div>
...
- Under some conditions, the following line of code executed
meetingObject.SIteGID = 5; // Force the selection of a specific Branch by Code
- After the step (2), I would like to return to inititial state. That means, show the message to select a branch, raise validations if nothing is selected on Submit. for Example
...
meetingDTO.SIteGID = "Select"; // This line of code does not raise annotation messages if the user ignore the selection.
Any help will be great. Thank you
...
meetingDTO.SIteGID = "Select"; // This line of code does not raise annotation messages if the user ignore the selection.
Here's a form loosely based on the information provided in your question, that demonstrates one way to code what I believe you are asking for.