39

I have a parent component and a child component with an event callback where I want it to pass back a type of T.

The issue I'm having is that I have an error about converting

MethodGroup to EventCallback.

If I convert this to using an Action then it works, but I can't do it async which isn't ideal.

Any ideas what I'm doing wrong?


Parent

<Child DeleteCallback="@OnDelete"></Child>

public async Task OnDelete(T item)
{ ... }

Child

@typeparam T

[Parameter]
public EventCallback<T> DeleteCallback { get; set; }

 <a @onclick="() => DeleteCallback.InvokeAsync(item)"></a>

I've added a repo here explaining the problem. Looking at the Issues for Blazor, this should;ve been fixed in 2019. https://github.com/scott-david-walker/EventCallbackError

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
scottdavidwalker
  • 1,118
  • 1
  • 12
  • 30
  • 1
    @Jean-François Fabre can you please unhide the post in this page? It is one of the right answers – samayo Mar 29 '22 at 12:10

12 Answers12

62

You were close:

<ChildComponent Item="someModel" T="SomeModel" DeleteCallback="OnDeleteSomeModel" />

@code {
    SomeModel someModel = new SomeModel();

    void OnDeleteSomeModel(SomeModel someModel)
    {
        ...
    }
}

The EventCallback uses a generic type and blazor cannot infer it if you don't pass the type to the component.

This means that if you have a EventCallback<T> you need to pass the value of T to the component e.g. T="SomeType".

Vencovsky
  • 28,550
  • 17
  • 109
  • 176
Brian Parker
  • 11,946
  • 2
  • 31
  • 41
  • 4
    I found the issue its because your using a templated component it is having trouble with the public EventCallback DeleteCallback, You need to declare the type of T not just infer it by usage. I'll update my answer – Brian Parker Oct 12 '20 at 09:32
  • 2
    That did the trick, cheers for the help. That was an annoying one :-) – scottdavidwalker Oct 12 '20 at 11:15
  • 4
    Would be great to get a more specific error message that actually points us to the issue. – codah Jul 29 '21 at 00:17
  • For me it was that I had a non-generic `EventCallBack` inside a custom component, and the error was thrown from outside the component. Changing that to `EventCallBack` fixed the issue. – alelom Feb 15 '23 at 19:44
15

In my case I declared a nullable EventCallback? - you can't do that.

Sebazzz
  • 1,205
  • 2
  • 15
  • 33
7

I've experienced that the syntax was spot on and still getting this error.

Restarting Visual Studio 2019 solved the problem for me. Cleaning and rebuilding was not enough.

JTvermose
  • 322
  • 4
  • 13
  • Same experience here. I replaced one class with another (correct) class, and got this issue. Rebuilding, cleaning, reloading the project doesn't remedy the problem, only a restart of VS2019. This is one of many errors in Blazor that can be solved this way – Christoph B Apr 22 '21 at 07:59
  • 1
    VS often shows Blazor relater errors in the Errors List that aren't actually true. Check the Output window for more accurate info. Not sure in this case though. – codah Jul 29 '21 at 00:19
7

For some reason Visual Studio kept a previous signature I had used. Cleaning, restarting, emptying bin/obj folders etc did not work. I had to rename the method, which worked for me.

Kristoffer Jälén
  • 4,112
  • 3
  • 30
  • 54
6

The following syntax worked for me:

// in the component (C#):
[Parameter]
public EventCallback<MovingEventArgs> OnMoving { get; set; }

// on the using side (Razor):
OnMoving="(Component.MovingEventArgs o) => OnMoving(o)"

// on the using side (C#):
protected void OnMoving( Component.MovingEventArgs e ) {
}

There seems to have been a change and the documentation is not up to date. This event is also using custom event arguments.

cskwg
  • 790
  • 6
  • 13
3

In my case, the problem solved when I defined the @typeparam manually(not by inference)(TItem="int").

<MyComponent TItem="int" OnChange="Component_Changed" />
nAviD
  • 2,784
  • 1
  • 33
  • 54
  • This deserves more than an up-vote. Setting the type is frequently overlooked, and very view samples include it. If you are using the "changed" event, instead of binding directly, setting the type is almost always required. – Brad Bruce Jun 22 '23 at 19:39
3

For anyone fighting this with the MudBlazor MudTable I found this worked use @bind-SelectedItem and don't pass any parameters to the commit function.

<MudTable T="POCO" Items="@MyState.POCOs" 
                              @bind-SelectedItem="_selectedPOCO"  
                              OnCommitEditClick="@(() => commitPOCO())"
                              CommitEditTooltip="Save Changes?">
@code {
    private POCO _selectedPOCO;

    private async void commitPOCO()
    {
        // all the changed values are in _selectedPOCO
        ...
    }
EricS
  • 45
  • 1
  • 1
1

After trying all other solutions proposed here, including:

  • Making sure I was providing T=myType
  • Clean and rebuild
  • Restart Visual Studio
  • Rename the method by appending a letter to its name and aligning its binding

The only thing that worked for me was literally deleting the line with the binding, saving, recompiling, then writing it back.

In doing so, I in fact realized that I was typing:

SelectedItemChanged="@SelectedItemsChanged"

instead of:

SelectedItemsChanged="@SelectedItemsChanged"

I was missing an s in the binding. The method itself had a signature receiving a HashSet of my type, so it could only deal with Items, not a single item.

alelom
  • 2,130
  • 3
  • 26
  • 38
0

Okay I just spent hours trying to figure this out. Turns out on my end the problem was I have sync fusion in the project and I had explicitly define the name space of the Event call back arguments which in my case were Microsoft.AspNetCore.Components.ChangeEventArgs in the child component and then creating an async method to pass back data to the parent.

0

In my case it was a phantom error among tens others that never went away no matter what I do. Meaning I could still build and run a solution without problems, despite so many erorrs.

What helped me was this solution: https://stackoverflow.com/a/66219566/1215913

However, I finally found a solution after a few hours of digging. To get rid of the phantom errors I closed Visual Studio, deleted the files in the following folder, and then re-opened the solution: C:\Users<Username>\AppData\Local\Temp\VSFeedbackIntelliCodeLogs\Suggestions\

The only difference is I removed all of the folders that start with VS (C:\Users\<Username>\AppData\Local\Temp\VS*) since there was no this exact path as @rovert mentions in his post.

This seems to be some common problem with Blazor and IntelliSense. I also often see that auto-formatter fails to correctly format the razor code (text indentations are a mess).

Alex
  • 4,607
  • 9
  • 61
  • 99
0

In my case I got the error

Error (active) CS1503 Argument 2: cannot convert from 'method group' to 'EventCallback'

for ValueChanged with the following code:

<MatAutocompleteList TItem="UseCase" Items="@selectUseCases" ValueChanged=@SelectUseCases>
                        <ItemTemplate>
                            @context?.Label
                        </ItemTemplate>
                    </MatAutocompleteList>

@code
{
    private UseCase SelectUseCases(UseCase useCaseDto)
    {
        if (useCaseDto != null)
        {
            //Do stuff
            
        }
        return null;
    }
}

Changed to private void SelectUseCases(UseCase useCaseDto), removed all returns and the error disappeared.

Ogglas
  • 62,132
  • 37
  • 328
  • 418
0

The following Solution is working for me.

<InputSelect ValueChanged="@((string s) => DoThing(s))" ValueExpression="@(()=> form.MyProperty)"> <option value="0">0</option> <option value="100">100</option> <option value="200">200</option> <option value="300">300</option> </InputSelect>

`@code { MyForm form = new MyForm();

void DoThing(string s)
{
    form.MyProperty = s;
}

public class MyForm
{
    public string MyProperty { get; set; }
}

}'

sourcecode
  • 4,116
  • 1
  • 20
  • 14