0

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?

Chris Gergler
  • 168
  • 11

1 Answers1

2

try to type it like this with an arrow function

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

This should work.

  • 1
    Thank you, is there any reason why I need the lambda expression over just putting the function? – Chris Gergler Jan 27 '22 at 19:51
  • 2
    The onclick method expects a delegate type. You can read this post for more detailed information https://stackoverflow.com/questions/67758441/why-is-a-lambda-function-needed-to-pass-a-value-to-a-method-called-by-onclick – CodeStallion Jan 27 '22 at 19:55