我在我的项目中使用了MatBlazor框架。在MatSelect中,我希望捕捉它在onchange事件上的值,以执行其他一些工作。我已经尝试了一些解决方案,但是onchange事件还没有启动。
<MatSelect Label="Customer" Value="@customer" ValueChanged="OnChangeCustomer">
<MatOptionString Value="-1">All</MatOptionString>
@foreach (var item in customers)
{
<MatOption Value="@item.Id">@item.Name</MatOption>
}
</MatSelect>下面是我的onchange事件处理程序。但是,当在下拉列表中选择另一个值时,它没有触发:
public void OnChangeCustomer(ChangeEventArgs args)
{
if (args.Value.ToString() != "-1")
isAccountDropDownListDisabled = false;
}有谁可以帮我?谢谢
发布于 2020-11-13 08:38:58
您可以将以下示例引用到使用MatSelect控件:
<MatSelect Outlined="true" Label="Category" ValueChanged="(string i) => OnChangeCategory(i)">
<MatOptionString Value="-1">All</MatOptionString>
@foreach (var cat in GetCategories())
{
<MatOptionString Value="@cat.Id.ToString()">@cat.Name</MatOptionString>
}
</MatSelect>
<span>@selectedValue</span>
@code
{
public string selectedValue;
protected List<Customer> GetCategories()
{
//return new List<string>() { "AA", "BB" };
return new List<Customer>() {
new Customer(){Id=1001, Name="Tom"},
new Customer(){Id=1002, Name="David"},
new Customer(){Id=1003, Name="Lucy"}
};
}
protected void OnChangeCategory(string value)
{
//do something
selectedValue = "Selected Value: " + value;
}
}截图如下:

更详细的信息,查看MatSelect文档。
发布于 2021-03-08 21:40:25
@ZhiLv的代码运行良好,但是如果您想要一个预先填充的动态选择值,它将变得更加困难。
我花了这么多小时试图让它与MatSelectValue一起工作,却没有任何运气。
https://www.matblazor.com/SelectValue
最后,我使用了一个简单的MatSelect,该属性调用了我的onchange event方法。这是我得到正确预填的选择列表的唯一方法。
使用可空int的示例,但也可以更改为string、guid等。
https://www.matblazor.com/Select#MatSelectGuid
@inject StateContainer StateContainer
<MatSelect Label="Choose threat" @bind-Value="@SelectThreatId" Outlined="true" FullWidth="true">
@foreach (var item in selectThreats)
{
<MatOption TValue="int?" Value="@item.Id">@item.Threat</MatOption>
}
</MatSelect>
@code
{
[Parameter]
public ThreatAndCountermeasureDto ThreatAndCountermeasureDto { get; set; }
List<ThreatDto> selectThreats = new List<ThreatDto>();
ThreatDto selectedThreat = null;
private int? threatId = null;
public int? SelectThreatId
{
get { return threatId; }
set
{
threatId = value;
SelectThreatValueChanged(value);
}
}
private void SelectThreatValueChanged(int? id)
{
selectedThreat = StateContainer.Threats.Single(x => x.Id == id);
}
protected override void OnInitialized()
{
base.OnInitialized();
StateContainer.OnChange += StateHasChanged;
SelectThreatId = ThreatAndCountermeasureDto.Threat.Id;
selectThreats = StateContainer.Threats.ToList();
}
...来源:
https://stackoverflow.com/questions/64802201
复制相似问题