Home > Software design >  How to check an InputRadio radio button in Blazor by default
How to check an InputRadio radio button in Blazor by default

Time:09-25

Using Blazor Server, I am trying to setup some radio buttons within a form and check the first one by default.

I'm using InputRadio within an EditForm and setting checked="checked" or checked="true" but it's not working, the button is unchecked.

Sample code below. TestModel is a class with a single int property called Number.

@page "/Test"

<div>
    <EditForm Model="@_model">
       <InputRadioGroup @bind-Value="_model.Number"> 
            <div>
                <InputRadio id="1radio" Value="1" checked="checked" />
                <label for="1radio">1</label>
            </div>

            <div>
                <InputRadio id="2radio" Value="2" />
                <label for="2radio">2</label>
            </div>
        </InputRadioGroup>

    </EditForm> 
</div>


@code {
    private TestModel _model = new();
}

CodePudding user response:

Instantiate your model with the default Number value set.

private TestModel _model = new() { Number = 1 };
  • Related