I am trying to bind the value of a radio button in .NET 5.0 Blazor WASM Hosted project. The code is simple but it is not working. The value of anyValue
remains "false" after submitting the form.
Parent Component
<EditForm Model="model" OnValidSubmit="OnSubmitForm">
<RadioButton Name="TestRadio" Text="Choose value" Value="@anyValue" />
</EditForm>
@code {
string anyValue = "false";
......................
private async Task OnSubmitForm()
{
model.TestRadio = Convert.ToBoolean(anyValue);
}
......................
}
RadioButton Component
<div class="form-group">
<label>
@Text
</label>
<div>
<InputRadioGroup Name="@Name" @bind-Value="@Value" class="form-control col-sm-4">
<InputRadio Name="@Name" Value="@trueVal" />Yes<br>
<InputRadio Name="@Name" Value="@falseVal" />No<br>
</InputRadioGroup>
</div>
</div>
@code {
string trueVal = "true";
string falseVal = "false";
[Parameter]
public string Text { get; set; }
[Parameter]
public string Name { get; set; }
[Parameter]
public string Value { get; set; }
}
What am I doing wrong and how to fix it? I do not like the code myself but Blazor just has a strange way of handling these…
2
Answers
This is the right way:
You can put the model on another layer
Component:
Use in parent razor page: