Aspnetcore: cannot convert lambda expression to type 'EventCallback' because it is not a delegate type

Created on 5 Sep 2019  路  4Comments  路  Source: dotnet/aspnetcore

currently preview9 EventCallbacks for onclick handlers do not work as expected:

<ul class="list-group">
@foreach (var project in _projects)
{
    <li class="list-group-item @(_activeProject?.Id == project.Id ? "active" : string.Empty)" @onclick="@(() => SelectProject(project))"> <!-- @(e => doesn't work, too, or trying to use an async variant: @(async => await SelectProject)
       @project.Name
    </li>
}
</ul>

@code
{
    private async Task SelectProject(GitlabProject project)
    {
    }
}

will raise
cannot convert lambda expression to type 'EventCallback' because it is not a delegate type
(https://docs.microsoft.com/de-de/aspnet/core/blazor/components?view=aspnetcore-3.0#lambda-expressions)

area-blazor question

Most helpful comment

To fix this migration issue, you may need to add below line to the _Imports.razor file

@using Microsoft.AspNetCore.Components.Web

All 4 comments

To fix this migration issue, you may need to add below line to the _Imports.razor file

@using Microsoft.AspNetCore.Components.Web

Thanks for contacting us, @schmitch.
@royzhang2016 's proposal should fix your issue.

I'm still seeing that error message despite having that using statement. Are there any other possible causes?

@Grauenwolf I had the same error. It turned out, I missed "@" before the "onclick". So it must have two "@": ...@onclick="@( ... )..

Was this page helpful?
1 / 5 - 1 ratings