在尝试更新select中的列表选项时遇到问题。当点击Function One
按钮时,它会触发functionTwo
,返回一个新的列表,现在应该是<select>/<option>
中显示的更新列表
有人能教育我什么地方我做得不对吗?
<button ... @onclick="functionOne">Function One</button>
<select @bind="@SelectedValue">
@foreach (var val in updatedList)
{
<option value="@val">@val</option>
}
</select>
public void functionOne()
{
...
List<string> updatedList ...
updatedList = functionTwo();
...
}
public List<string> functionTwo()
{
...
return strList ;
}
将其复制到blazor的index.razor中,看看它是否有效
@page "/"
<p>@SelectedValue</p>
<button @onclick="functionOne">Function One</button>
<select @bind="@SelectedValue">
@foreach (var val in updatedList)
{
<option value="@val">@val</option>
}
</select>
@code
{
string SelectedValue { get; set; }
List<string> updatedList = new List<string>();
private void functionOne()
{
updatedList = functionTwo();
}
public List<string> functionTwo()
{
return new List<string> { "AAA", "BBB", "CCC" };
}
}