Page is refreshed after the second clicking?

Viewed 364

I have the following blazor page to display a page of filtered result from database.

@inject ViewModels.MyViewModel VM
<input id="search" type="text" @bind="search" />
<input id="search" type="button" value="Go" @onclick="SearchChanged" /> 
<p>Current count: @currentCount</p>  
@code {
    string search = "";
    int currentCount = 0;
    IEnumerable<MyDto> dtos;

    async void SearchChanged() {
        currentCount++;
        Console.WriteLine($"VM.Ric: {ric}"); // Correct values will be printed
        dtos = GetList(search);
    }
}
@if (dtos == null)
{
    <p><em>Please enter the ...</em></p>
}
else
{
    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var ca in dtos)
            {
                <tr>
                    <td>@ca.ID</td>
                </tr>
            }
        </tbody>
    </table>
}

However, I need to click the button twice (after entered a ) to see the result. For example, clicking the button after entered "ABC" will not refresh the screen. Clicking again will show the correct value.

1 Answers

You should use Task instead of void in

async void SearchChanged()

Use this: async Task SearchChanged()

Related