Carlos Jimenez Bermudez
Carlos Jimenez Bermudez

Reputation: 1175

Blazor Server simple onchange event does not compile

So I want to do a plain and simple onchange event. When the user changes an option in a dropdown that is not part of any form, I want it to change my Store class (which is a stand-in for what you would do with React-Context).

So I have my component here:

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

and my separate component code file here:

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();
        }
    }
}

But no matter what I do, I get the following compiler errors, despite the fact that every single tutorial or example seems to work without these errors.

Compiler errors

If I remove the event parameter from the function then everything seems to compile. But I need the id of the restaurant that the app is changing to, so having a blind function run on change is pointless to me.

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?

Upvotes: 1

Views: 464

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273169

// 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.

Upvotes: 3

Related Questions