I am writing a blazor app, where the user has to select a group of options in the form of a radio buttons. This selection needs to be done inside a component. Which also meant, I needed to pass data from a radio group in a child component to a parent component.
The Test
Below are code snippets for both child and parent components. The test involves selecting one of the four seasons (spring, summer, autum, and winter) in the child component. When the user selects any of the four seasons, that selection needs to be updated in the parent component as well.
Attempt
First attempt was to use an Editform.
This is the child component. Pages\Child.razor
<h1>Child</h1>
EditForm Model="model">
<InputRadioGroup @bind-Value="SelectedValue">
@foreach (var revOption in (Season[])Enum
.GetValues(typeof(Season)))
{
<InputRadio Value="@revOption" @onchange="@UpdateSelection"/>
<text> </text>
@revOption <text> </text>
}
</InputRadioGroup>
<p>Selected Season inside Child: @SelectedValue</p>
</EditForm>
@code {
[Parameter]
public string SelectedValue { get; set; } = "No seasons selected.";
[Parameter]
public EventCallback<string> TestEvent { get; set; }
Model model = new Model();
class Model
{
public Season Season { get; set; }
}
enum Season
{
Spring,
Summer,
Autumn,
Winter
}
private async Task UpdateSelection()
{
SelectedValue = model.Season.ToString();
await TestEvent.InvokeAsync(SelectedValue);
}
}
and the parent component index.razor
<h1>Parent</h1>
<Child TestEvent=@UpdateChildSeason />
<p>Season data from Child: @ChildSeason</p>
@code
{
string ChildSeason;
void UpdateChildSeason(string value)
{
ChildSeason = value;
}
}
This does not work properly. I not pass the data to the parent
p.s. At the same time, if I select any of the radio buttons, all of the radio buttons will be cover in green buttons.
I have no idea why that happens.

So, the solution I have is to not use
EditForm. Well, since I am not worried about data validation, I can do away withEditFormfor now.Here is my solution.
This is the child component.
Pages\Child.razorand the parent component
index.razorNow, the data is finally passed to the parent. This will work for me now. Does anyone have a solution for the
EditFormversion and how to remove that green boxes from the radio buttons?