我需要能够在<select>. 问题是我也绑定了@bind,当我尝试使用@onchange 时出现错误,指出@bind 已经在使用它。我尝试使用@onselectionchange,但这没有任何作用(不运行该功能)。我可以忘记@bind 而只是将@onchange 分配给一个函数,但我不确定如何将选定的值传递给该函数。
我有以下代码:
<select @bind="@SelectedCustID" @ @onchange="@CustChanged" class="form-control">
@foreach (KeyGuidPair i in CustList)
{
<option value="@i.Value">@i.Text</option>
}
</select>
Run Code Online (Sandbox Code Playgroud)
谢谢。
我在某些库(MatBlazor、Telerik)中看到了这种具有ValueChanged和ValueExpression属性的常见模式,这让我很困惑。
两者有什么区别?什么时候使用它?
在这里,我使用 blazor 服务器应用程序,并尝试使用事件根据国家/地区下拉列表中的更改填充城市下拉列表@onchange="countyClicked",并将下拉列表与模型绑定。
现在的问题是 onchange 事件不起作用,并且城市下拉列表不会填充到国家/地区下拉列表的 onchange 上。
下面是我的下拉列表组件 html
<InputSelect @bind-Value="PersonModel.CountryId" class="form-control" @onchange="countyClicked">
<option value="">Select country</option>
@foreach (var item in Countries)
{
<option value="@item.CountryId">@item.CountryName</option>
}
</InputSelect>
<InputSelect class="form-control mb-2 mr-sm-2" @bind-Value="PersonModel.CityId">
@foreach (var city in Cities)
{
<option value="@city.CityId">@city.CityName</option>
}
</InputSelect>
<br><br>
public void countyClicked(ChangeEventArgs args)
{
var getCountryId = args.Value.ToString();
int.TryParse(getCountryId, out int countryId);
Cities = mainService.GetAllCityByCountryId(countryId);
}
Run Code Online (Sandbox Code Playgroud)