【发布时间】:2022-01-25 10:32:52
【问题描述】:
我有 2 个级联下拉菜单:
问题是当我点击Save 按钮时,我的模型将为空。我不能在我的下拉列表中使用bind-value,因为我不能使用onchange 事件。只要绑定属性,我如何才能拥有级联功能。
代码:
@using Model
<select value="@MyLocation.CountryCode" @onchange="@CountryChanged">
@if (string.IsNullOrEmpty(MyLocation.CountryCode) == true)
{
<option value="0" selected>[Choose One...]</option>
}
@foreach (var item in Countries)
{
<option value="@item.Key">@item.Value</option>
}
</select>
<br />
<select value="@MyLocation.CityCode">
@foreach (var item in CountryCities)
{
<option value="@item.Key">@item.Value</option>
}
</select>
<br />
<button class="btn btn-success" @onclick="() => HandleSave()">Save</button>
@code {
public Location MyLocation { get; set; } = new Location();
private Dictionary<string, string> Countries { set; get; } = new Dictionary<string, string>();
private Dictionary<string, string> CountryCities { set; get; } = new Dictionary<string, string>();
protected override Task OnInitializedAsync()
{
Countries.Add("01", "Germany");
Countries.Add("02", "Japan");
Countries.Add("03", "England");
return base.OnInitializedAsync();
}
private async Task CountryChanged(ChangeEventArgs e)
{
CountryCities.Clear();
switch (e.Value.ToString())
{
case "01":
CountryCities.Add("001", "Munchen");
break;
case "02":
CountryCities.Add("002", "Tokyo");
break;
case "03":
CountryCities.Add("003", "Manchester");
break;
}
}
private void HandleSave()
{
var Country = MyLocation.CountryCode; <--- CountryCode is null
}
}
【问题讨论】:
标签: c# blazor blazor-webassembly