Aspnetcore: Async function: The message is only updated on the second time we click the button

Created on 27 Mar 2018  路  2Comments  路  Source: dotnet/aspnetcore

The message is only updated on the second time we click the button that calls DownloadData

@page "/counter"
@using Flurl
@using Flurl.Http
@inject HttpClient Http

<h1>Download Data</h1>

<button @onclick(DownloadData)>Download Data</button>

<p><b>@message</b></p>

@functions {

string message = "";

async void DownloadData()
{
    try
    {
        var url = "https://nnnn.azurewebsites.net/api/PegaOcorrencias/?code=kjsdfklhsdf";
        var result = await Http.GetJsonAsync<List<Newtonsoft.Json.Linq.JObject>>(url);

        message = "Items count " + result.Count;
    }
    catch (Exception ex)
    {
        message = ex.ToString();
    }

}

}

area-blazor

Most helpful comment

@onclickAsync isn't implemented yet, you'd need to call StateHasChanged() at the end of the function call to notify the component to update it's State for now. Or you could do it this way as well.

@onclick( async () => await DownloadData())

async Task DownloadData() {
//...
}

All 2 comments

@onclickAsync isn't implemented yet, you'd need to call StateHasChanged() at the end of the function call to notify the component to update it's State for now. Or you could do it this way as well.

@onclick( async () => await DownloadData())

async Task DownloadData() {
//...
}

Thanks for answering, @RyoukoKonpaku!

Was this page helpful?
0 / 5 - 0 ratings