CS0428 C# Cannot convert method group to non-delegate type 'object'. Did you intend to invoke the method?

Viewed 9919

Im getting this compiler error in Blazor ("CS0428 C# Cannot convert method group to non-delegate type 'object'. Did you intend to invoke the method?") and I seriously do not know how to fix it or which route to take. the error happens on both onchange events of the drop-down lists. please help!

<select class="form-control" onchange="**@ReportingYearClicked**" style="width: 95px">
   <option value="">-- Select Reporting Year --</option>
    @foreach (var year in @yearlist)
    {
      <option value="@year">@year</option>
    }
   </select>
 
<select class="form-control" onchange="**@ClientClicked**" style="width: 600px">
   <option value="">-- Select Client --</option>
   @if (ReportingYear == "ALL")
   {
    @if (client != null)
    {
     @foreach (var client in @client)
     {
      <option value="@client.ClientUno">@client.ClientUno &nbsp; @client.ClientName</option>
     }
    }
   }
   else
   {
    @if (clientListDto != null)
    {
     @foreach (var client in @clientListDto)
     {
      <option value="@client.ClientUno">@client.ClientUno &nbsp; @client.ClientName</option>
     }
    }
   }
</select>

@Code
{
protected override async Task OnInitializedAsync()
{
DateTime now = DateTime.Today;
    currentyear = now.ToString("yyyy");
    YearList(currentyear);
    client = await Http.GetJsonAsync<List<ClientDto>>("http://XXXXXXX/api/clients");
  }

  protected async void ReportingYearClicked(ChangeEventArgs clientEvent)
  {
    client.Clear();
    clientListDto.Clear();
    clientListDto = await 
      Http.GetJsonAsync<List<ClientListDto>>"api/clientListReportingYear="+                                                                                       
                                                                            ReportingYear);
        
this.StateHasChanged();
  }

    void ClientClicked(ChangeEventArgs clientEvent)
    {
        ReportingYear = clientEvent.Value.ToString();
        this.StateHasChanged();
    }
}
2 Answers

You have to change the code where you are calling the onchange event to @onchange:

<select class="form-control" @onchange="@ReportingYearClicked" style="width: 95px">

I have this problem i a Razor view with this.

@line.GetExtraField<DateTime>("DeliveryDate").ToShortDateString()

then I changed it to this and it works.

@(line.GetExtraField<DateTime>("DeliveryDate").ToShortDateString())
Related