Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async Lamblda Expression in Razor page of Blazor Server App

I have an async method in razor page of a blazor server app.

What I am trying to achieve is as below but of course await requires async. How can I await async method here?

@{
    var result = await Test(local);    
    <div>
        @result
    </div>
}

This does not work. It says "cannot convert lambda expression to type 'object' because it is not a delegate type".

<div class="line">
    @(async (local) => { await Test(local); })
</div>
like image 269
Demir Avatar asked Dec 06 '25 07:12

Demir


1 Answers

It's better to use component life cycle events. You can to do something like

<div class="line">
    @result
</div>

@code{
    private string result;

    protected async override Task OnInitializedAsync()
    {
        result = await Test(local);
    }
}

See Blazor documentation here

like image 152
Shahid Syed Avatar answered Dec 08 '25 22:12

Shahid Syed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!