Search code examples
htmlasp.net-blazor

error CS1503 cannot convert from 'method group' to 'EventCallback' in blazor


<div>
    <InputeFile class="form-control" @onchange="SelectBanner"></InputFile>
</div>

I want to get full path of file from input in blazor page
but I have this error:
cannot convert from 'method group' to 'EventCallback' in blazor

and this is my method that should get full path of selected file:

@code {
           protected string banner;

           protected async Task SelectBanner(InputFileChangeEventArgs e)
           {
               banner = e.File.Name;
           }
      }

how can I fix this error?


Solution

  • You are using wrong Event callback, it should be

    OnChange

    instead of

    @onchange

    so your code should look like this

    <div>
        <InputFile class="form-control" OnChange="@(async e=>await SelectBanner(e))"></InputFile>
    </div>
    
    @code {
        protected string banner;
    
        protected async Task SelectBanner(InputFileChangeEventArgs e)
        {
            banner = e.File.Name;
        }
    }