Blazored / Typeahead

Typeahead control for Blazor applications
https://blazored.github.io/Typeahead/
MIT License
444 stars 103 forks source link

Downloaded Blazored.Typeahead 4.1 & received the following error on compile #64

Closed ironbull45 closed 5 years ago

ironbull45 commented 5 years ago

I used the example found here: https://chrissainty.com/getting-started-with-blazored-typeahead/

@page "/typeahead"

<h3>Typeahead</h3>

<BlazoredTypeahead SearchMethod="SearchFilms"
                   @bind-Value="SelectedFilm">
    <SelectedTemplate>
        @context.Title
    </SelectedTemplate>
    <ResultTemplate>
        @context.Title (@context.Year)
    </ResultTemplate>
</BlazoredTypeahead>

@if (SelectedFilm != null)
{
    <p>Selected Film is: @SelectedFilm.Title</p>
}

@code {

    private List<Film> Films;
    private Film SelectedFilm;

    protected override void OnInitialized()
    {
        Films = new List<Film> {
            new Film("The Matrix", 1999),
            new Film("Hackers", 1995),
            new Film("War Games", 1983) };
    }

    private async Task<List<Film>> SearchFilms(string searchText)
    {
        return await Task.FromResult(Films.Where(x => x.Title.ToLower().Contains(searchText.ToLower())).ToList());
    }

    class Film
    {
        public string Title { get; set; }
        public int Year { get; set; }

        public Film(string title, int year)
        {
            Title = title;
            Year = year;
        }
    }

}

During compile, I received the following errors:

typeahead_errors

The razor component doesn't show any errors until compile time.

ironbull45 commented 5 years ago

This is error in the .cs file on line 110:

typeahead_errors_2

Sapporun commented 5 years ago

Hey, I managed to solve it by changing the code (Your Typeahead.razor file) into following:

private IEnumerable<Film> Films;

    private async Task<IEnumerable<Film>> SearchFilms(string searchText)
    {
        return await Task<IEnumerable<Film>>.FromResult(Films.Where(x => x.Title.ToLower().Contains(searchText.ToLower())).ToList());
    }
}

@ironbull45

ironbull45 commented 5 years ago

Thank you. I saw that in the original comments as well.