O evento onchange simples do Blazor Server não compila

Aug 30 2020

Então eu quero fazer um evento onchange puro e simples. Quando o usuário altera uma opção em um menu suspenso que não faz parte de nenhum formulário, quero que ele altere minha classe Store (que é um substituto para o que você faria com o React-Context).

Então eu tenho meu componente aqui:

<select @onchange="OnRestaurantChanged">
    @foreach (var restaurant in _restaurantStore.State.RestaurantList)
    {
        <option value="@restaurant.Id">@restaurant.Name</option>
    }
</select >

e meu arquivo de código de componente separado aqui:

using MenuApp.Stores.Restaurant;

namespace MenuApp.Pages.Components.Restaurants
{
    public partial class RestaurantSelector : ComponentBase
    {
        [Inject]
        private IRestaurantStore _restaurantStore { get; set; }

        public string RestaurantId { get; set; }

        private void OnRestaurantChanged(ChangeEventArgs event)
        {
            _restaurantStore.ChangeSelectedRestaurant();
        }
    }
}

Mas não importa o que eu faça, recebo os seguintes erros do compilador, apesar do fato de que cada tutorial ou exemplo parece funcionar sem esses erros.

Se eu remover o parâmetro de evento da função, tudo parece compilar. Mas eu preciso do id do restaurante para o qual o aplicativo está mudando, então ter uma função cega executada na mudança é inútil para mim.

I was thinking of binding to a variable and then using the onChange to send the bound variable to my store function, but I think that double binding is not only unnecessary but might cause other problems.

How can I access the ChangeEventArgs?

Respostas

3 HenkHolterman Aug 30 2020 at 04:23
// private void OnRestaurantChanged(ChangeEventArgs event)
   private void OnRestaurantChanged(ChangeEventArgs @event)

event is a reserved word. You can 'escape' that with a @. General C# rules, nothing to do with Razor.