我有一个由三个单选按钮组成的列表,我正在想办法显示哪一个被点击并保存了。这些单选按钮在我的Blazor项目中,单击时的UpdateFilter方法只会将字符串保存到数据库中。如何使单击并保存的页面在加载页面时显示为选中?
<label class="GroupTitle">Filters:</label><br/>
<input type="radio" name="number" id="all" @oninput="@(() => UpdateFilter("all"))" />
<label for="">Clear all files</label><br />
<input type="radio" name="number" id="number" @oninput="@(() => UpdateFilter("number"))" />
<label for="">Based on number of files</label><br />
<input type="radio" name="number" id="date" @oninput="@(() => UpdateFilter("date"))" />
<label for="date">Based on date of files</label><br />
您确实应该使用<InputRadioGroup>
组件。这里有一个非常做作的例子:
<EditForm Model="this.model">
<InputRadioGroup @bind-Value="this.model.Filter">
@foreach (var filterOption in this.FilterOptions)
{
<label><InputRadio Value="@filterOption.Key" /> @filterOption.Value</label>
<br />
}
</InputRadioGroup>
</EditForm>
@code{
private Model model = new Model();
private Dictionary<string, string> FilterOptions = new
{
{ "all", "Clear all files" },
{ "number", "Based on number of files" },
{ "date", "Based on date of files" },
}
public class Model
{
public string Filter { get; set; }
}
}