Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor button event not firing

$
0
0

I am creating a Blazor Server app using the Amadeus SDK and I cannot trigger LoadFlights(), which I define in the razor.cs file, when pressing a button with the method assigned to it.Home.cs

@page "/"@using sexy.Components.Shared@using sexy.Models<PageTitle>Home</PageTitle><MudPaper Class="p-4" Elevation="4"><MudButton @onclick="LoadFlights" Color="Color.Primary" Variant="Variant.Filled" Class="mt-2">Search Flights</MudButton>    @if (!string.IsNullOrEmpty(testMessage))    {<MudText Color="Color.Secondary" Class="mt-2">@testMessage</MudText>    }    @if (flights != null && flights.Count > 0)    {<h2>Flights:</h2><ul>            @foreach (var item in flights)            {<li>                    @item.Origin to @item.Destination, Departure: @item.DepartureDate, Return: @item.ReturnDate, Price: @item.Price, Airlines: @string.Join(", ", item.Airlines)</li>            }</ul>    }</MudPaper>

Home.razor.cs

using Microsoft.AspNetCore.Components;using sexy.Models;using sexy.Components.Shared;using System;using System.Collections.Generic;using System.Threading.Tasks;namespace sexy.Components.Pages{    public partial class Home : ComponentBase    {        [Inject] public TravelAPI TravelAPI { get; set; }        public List<FlightData> flights = new();        public string testMessage = "";        public string origin { get; set; }        public string destination { get; set; }        protected async Task LoadFlights()        {            if (string.IsNullOrWhiteSpace(origin) || string.IsNullOrWhiteSpace(destination))            {                testMessage = "Please enter both origin and destination.";                flights.Clear();                return;            }            try            {                var token = await TravelAPI.ConnectOAuth();                flights = await TravelAPI.GetFlights(token, "MEL", "SYD");                testMessage = $"Loaded {flights.Count} flights.";            }            catch (Exception ex)            {                testMessage = $"Error: {ex.Message}";                flights.Clear();            }        }    }}

I have tried adding the method within the razor file itself (within a @code block), and the breakpoint withint LoadFlights() is not being called. I also tried using a normal instead of Blazor button and it has the same error. Also, if I run the method in OnInitializedAsync, the method LoadFlights() will be run upon page initialization.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>