Search code examples
blazorblazor-server-sideasp.net-blazor

Blazor - Function call is trying to be a callback


I'm currently in the process of learning Blazor Server, and I'm not sure why I'm getting the error I'm getting.

I'm getting CS1503 Argument2: cannot convert from 'void' to 'Microsoft.AspNetCore.Components.EventCallback' when entering a parameter for remove.

Edit

I realized why it's saying that it's a callback, but I'm not sure why it can't pass parameters back to the function in question.*



<main>

    
    <input type="text" @bind-value="inputText" />
    <button @onclick="add">Add</button>
    
    
        
    @foreach(string s in TestList)
    {
    <div background-color="blue" id="@TestList.IndexOf(@s)">
        @s
        <button @onclick="remove(TestList.IndexOf(s))"></button>
    </div>
    }
    


</main>

@code {
    public List<string> TestList = new List<string>() { };

    private int id;
    private string inputText{ get; set; }
    private AuthenticationState authState;


    public void add()
    {
        TestList.Add(inputText);
    }
    public void remove(int index)
    {
        TestList.RemoveAt(index);
    }
    //public List<Apps> OpenApps = new List<Apps>(){};

    private void LaunchApp() // Object Parameter
    {
        // Add to list of existing Apps
    }
}

Would anyone be able to tell me why it's trying to convert the type from a function to a callback event?


Solution

  • try to type it like this with an arrow function

    <button @onclick="() => remove(TestList.IndexOf(s))"></button>
    

    This should work.