MudRadioGroup not toggling

147 Views Asked by At

My razor page has radiogroup like

<MudRadioGroup T="string" SelectedOption="@selectedName" SelectedOptionChanged="OnSelectedOptionChanged">
                <MudRadio Option="@("Adam")" Color="Color.Primary">Index</MudRadio>
                <MudRadio Option="@("Greg")" Color="Color.Primary">Regex</MudRadio>
 </MudRadioGroup>
 @if (_isOk)
 {
      <MudTextField Value="@prop1" Label="txt1" />
      <MudTextField Value="@prop2" Label="txt2" />
  }
  else
  {
      <MudTextField Value="prop3" Label="txt3" />
  }

In code behind i bind radio group like below

 private string selectedName = string.Empty;
 private bool _isOk;
 protected override void OnInitialized()
 {
    if (string.IsNullOrEmpty(ScanRule.RegexRule))
    {
        selectedName = "Adam";
        _isOk= true;
    }
    else
    {
        selectedName = "Greg";
        _isOk= false;
    }
  }

 private void OnSelectedOptionChanged(string selectedOption)
 {
    _isOk= selectedOption == "Adam";
 }

If i try to change option button then nothing happens. When i set debug point inside OnselectedOptionChanged method and try to change option buttons then it comes inside it twice which is wrong i think as well.

1

There are 1 best solutions below

5
On BEST ANSWER

If you use SelectedOption and SelectedOptionChanged.

Then the handler method that you assign to SelectedOptionChanged i.e. OnSelectedOptionChanged will need to also handle changing of the value for the variable that you declare on SelectedOption i.e. selectedName

private void OnSelectedOptionChanged(string selectedOption)
 {
    selectedName= selectedOption;
    _isOk= selectedName== "Adam";
 }

This is done automatically if you use the @bind directive @bind-SelectedOption="selectedName".

MudBlazor Snippet