Reputation: 8295
I have an issue where a radio button selection is reset immediately after clicking it.
I have the following fields in a form:
I think what's happening is:
Code:
<InputText @bind-Value="_viewModel.Amount"></InputText>
<InputRadioGroup @bind-Value="_viewModel.SelectedOptionId" Name="name">
@foreach (var option in _viewModel.RadioOptions)
{
<InputRadio Name="name" Value="@option.Id.ToString()" />
<text> </text>@option.Name<br>
}
</InputRadioGroup>
How do you prevent this?
Upvotes: 0
Views: 558
Reputation: 8295
It turned out that there is an issue with the InputRadioGroup
control.
This GitHub issue describes the problem.
Basically, it will always rebuild its internals (So in your case the InputRadio components) due to an always-changing Key, resulting in the same kind of race condition as above.
The suggested workaround (which worked) is to implement a custom InputRadioGroup with the following added behaviour:
public class MyInputRadioGroup<TValue> : InputRadioGroup<TValue>
{
private string? _name;
private string? _fieldClass;
protected override void OnParametersSet()
{
var fieldClass = EditContext?.FieldCssClass(FieldIdentifier) ?? string.Empty;
if (fieldClass != _fieldClass || Name != _name)
{
_fieldClass = fieldClass;
_name = Name;
base.OnParametersSet();
}
}
}
I believe this will be addressed in .NET 7 as per this pull request
Upvotes: 0
Reputation: 273179
What is happening here is that you bind to the wrong type.
I assume that both RadioOption.Id and SelectedOptionId are of type int
.
That is fine, but then don't convert the Options' Ids to something else. It will hinder binding to the Selected value.
<InputRadio Name="name" Value="@option.Id.ToString()" /> -- wrong
<InputRadio Name="name" Value="@option.Id" /> -- ok
Upvotes: 1