带有选择下拉菜单的Blazor onchange事件

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允许您执行选择逻辑。

  • Blazor 与第一个版本相比发生了巨大变化......虽然我对此进行了测试并且运行良好,但 Blazor 的快速变化很可能最终导致此答案不再适用。如果发生这种情况,请告诉我,我会更新答案。 (3认同)
  • @Patrick Knott 此时“@functions”已被删除,取而代之的是“@code”。 (3认同)
  • 即使使用所有正确的语法,我也无法为 onchange 事件触发此事件。 (2认同)
  • 当前要求您执行&lt;select @ onchange = @ DoStuff&gt;注意on更改前面的@ (2认同)
  • 实际上 onchange 应该是@onchange,这是预览版 6 中的语法更改 - 它是 3.0 版本的最终事件处理程序语法。 (2认同)

Pan*_*wat 5

以上答案对我不起作用,出现编译错误。

下面是我的工作代码。

@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)