Gra*_*ta1 11 c# asp.net-core blazor
因此,当选择下拉列表值更改时,我一直试图获得一个简单的onchange进行触发。像这样:
<select class="form-control d-flex" onchange="(dostuff())">
@foreach (var template in templatestate.templates)
{
<option value=@template.Name>@template.Name</option>
}
</select>
Run Code Online (Sandbox Code Playgroud)
该方法被称为:
void dostuff()
{
Console.WriteLine("first spot is firing");
_template = templatestate.templates.FirstOrDefault(x => x.Name ==
_template.Name);
Console.WriteLine("second spot is firing");
}
Run Code Online (Sandbox Code Playgroud)
无论如何尝试重新定向,我得到的结果都是浏览器中的此错误。
Uncaught Error: System.ArgumentException: There is no event handler with ID 0
Run Code Online (Sandbox Code Playgroud)
有什么明显的关键我想念吗?因为我有一个按钮onclick事件,它在同一页面上也可以正常工作。
Ron*_*ono 27
作为设置 onchange 事件的替代方法,您可以将下拉列表绑定到属性并处理属性集中的更改。通过这种方式,您可以在同一过程中获得所有被选中的值,而无需转换对象值。另外,如果您已经在 select 上使用了 @bind,那么您也不能在它上面使用 onchange。
<select @bind="BoundID">
...
</select>
@code {
private int? _boundID = null;
private int? BoundID
{
get
{
return _boundID;
}
set
{
_boundID = value;
//run your process here to handle dropdown changes
}
}
}
Run Code Online (Sandbox Code Playgroud)
Pat*_*ott 13
您的答案应该在cshtml中:
<select onchange=@DoStuff>
@foreach (var template in templates)
{
<option value=@template>@template</option>
}
</select>
Run Code Online (Sandbox Code Playgroud)
然后,您的@functions应该看起来像:
@functions {
List<string> templates = new List<string>() { "Maui", "Hawaii", "Niihau", "Kauai", "Kahoolawe" };
string selectedString = "Maui";
void DoStuff(ChangeEventArgs e)
{
selectedString = e.Value.ToString();
Console.WriteLine("It is definitely: " + selectedString);
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以只使用绑定...
<select bind="@selectedString">
Run Code Online (Sandbox Code Playgroud)
但是onchange = @ DoStuff允许您执行选择逻辑。
以上答案对我不起作用,出现编译错误。
下面是我的工作代码。
@inject HttpClient httpClient
@if (States != null)
{
<select id="SearchStateId" name="stateId" @onchange="DoStuff" class="form-control1">
<option>@InitialText</option>
@foreach (var state in States)
{
<option value="@state.Name">@state.Name</option>
}
</select>
}
@code {
[Parameter] public string InitialText { get; set; } = "Select State";
private KeyValue[] States;
private string selectedString { get; set; }
protected override async Task OnInitializedAsync()
{
States = await httpClient.GetJsonAsync<KeyValue[]>("/sample-data/State.json");
}
private void DoStuff(ChangeEventArgs e)
{
selectedString = e.Value.ToString();
Console.WriteLine("It is definitely: " + selectedString);
}
public class KeyValue
{
public int Id { get; set; }
public string Name { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7891 次 |
| 最近记录: |