Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get return value from EventCallback in blazor?

My situation is this: I'm trying to implement and Autocomplete.

The Autocomplete will have a Parameter that will receive a string and return a IEnumerable<TValue>.

Here is an example of what I'm trying to do

Autocomplete.razor

@code {
    [Parameter]
    public SOME_TYPE GetItems { get; set; }

    async void Foo(){
        IEnumerable<TValue> items = await GetItems(SomeString);
        // do something with items
    } 
}

ParentComponent.razor

<Autocomplete TValue="SomeEntity"
              GetItems="@GetItems" />

@code {        
    SOME_TYPE GetItems(string name) {
        IEnumerable<SomeEntity> entity = await GetEntitys(name);
        return entity;
    } 
}

The problem is that I don't know what to put in SOME_TYPE. Should I use EventCallback? Action? What should I use?

I tried using EventCallback but looks like I can't get a return value from EventCallback? I have no idea.

like image 679
Vencovsky Avatar asked Dec 18 '22 13:12

Vencovsky


1 Answers

I just find out how to do it, I should use Func<string, Task<IEnumerable<TValue>>>.

[Parameter]
public Func<string, Task<IEnumerable<TValue>>> GetItems { get; set; }

And

public async Task<IEnumerable<Employee>> GetItems(string name) {
    IEnumerable<SomeEntity> entity = await GetEntitys(name);
    return entity;
} 
like image 104
Vencovsky Avatar answered Dec 24 '22 02:12

Vencovsky