Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.*

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel



<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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading