本文介绍了在blazor中单击按钮时执行异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个剃刀组件"项目.我正在尝试在按下按钮时执行异步方法,但仍无法弄清楚语法.

I created a "Razor Components" project. I am trying to execute an asynchronous method when pressing a button, but could not figure out the syntax yet.

这是我的 Index.razor :

@page "/"
@inject GenericRepository<Person> PersonRepository 

// ...

@foreach (var person in persons)
{
    <button onclick="@(() => Delete(person.Id))">❌</button>
}

@functions {

    // ...

    async void Delete(Guid personId)
    {
        await this.PersonRepository.Delete(personId);
    }
}

当我单击按钮时,什么也没有发生.我尝试了各种返回类型(例如Task)和其他东西,但无法弄清楚如何使其工作.如果需要提供更多信息,请告诉我.

When I click the button, nothing happens. I tried various return types (e.g. Task) and stuff, but cannot figure out how to make it work. Please let me know if I need to provide more information.

每个文档/教程仅适用于按钮单击时的非异步void调用.

Every documentation / tutorial only just works with non-async void calls on button click.

谢谢.

推荐答案

您需要正确地调用Delete方法,并使其返回Task而不是void:

You need to call the Delete method properly and make it return Task instead of void:

<button onclick="@(async () => await Delete(person.Id))">❌</button>

@functions {

    // ...

    async Task Delete(Guid personId)
    {
        await this.PersonRepository.Delete(personId);
    }
}

这篇关于在blazor中单击按钮时执行异步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 17:04