Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MudRadioGroup not toggling

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading