Hello,
You could listen to the value of the RadioButton to change the value of the Label before submitting the form. Please refer to the following minimized code snippet.
<InputRadioGroup ValueChanged="@((e) => OnRadiochange(e))" TValue="string" ValueExpression="() => SelectedValue">
@foreach (var item in Items)
{
bool Checked = false;
if (SelectedValue.Equals(item, StringComparison.OrdinalIgnoreCase))
{
Checked = true;
}
<div class="form-check">
<InputRadio Value="@item" class="form-check-input" checked=@Checked />
@item <br />
</div>
}
</InputRadioGroup>
<label>
@Price
</label>
@code{
public string SelectedValue { get; set; } = "CP";
public string Price{ get; set; }
public List<string> Items { get; set; } = new List<string> { "CP", "NotCp"};
private void OnRadiochange(object sender)
{
SelectedValue = (string)sender;
if (SelectedValue.Equals("CP"))
{
Price = "Find Selling Price";
}
else
{
Price = "Other";
}
StateHasChanged();
}
}
Update :
get this error "The delegate type could not be inferred"
I tested with your code and this error did not appear. This error is usually caused by a data binding error, please check your page for data binding errors.
I want one of the buttons checked by default.
InputRadio
does not have a Checked property, for InputRadioGroup
you need to set the initial value using Value
or @bind-value
. Please refer to the following code snippet.
<InputRadioGroup Value="@SelectedValue" disabled=@isdisabled ValueChanged="@((e) => OnRadiochange(e))" TValue="string" ValueExpression="() => SelectedValue">
@foreach (var item in Items)
{
<div class="form-check">
<InputRadio Value="@item" class="form-check-input" />
@item <br />
</div>
}
</InputRadioGroup>
// set the default value of Price.
public string Price { get; set; } = "Inverse Proportion Workers and Time Problems";
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.