dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.19k stars 9.93k forks source link

the name @onclick does not exist in current context #16130

Closed dotnetgik closed 4 years ago

dotnetgik commented 6 years ago

I am using the button but i am getting error as @onclick does not exist in current context

please help

dlr1 commented 6 years ago

It should be something like this

<button onclick="@AddComp">Add component</button>
dotnetgik commented 6 years ago

i was missing the Event Argument in the function Thanks dlr1 for the comment

void GetFruits(UIMouseEventArgs e) { Fruits.Add("Apple"); Fruits.Add("Mango"); Fruits.Add("Grapes"); }

This worked for me

davidlsharp1 commented 6 years ago

On these same lines. How can you pass a parameter through an onclick event? I cant get it to work but I am probably missing something.

I am getting the following error? Argument 1: cannot convert from 'System.Threading.Tasks.Task' to 'string'

I watched a demo here where the guy uses what looks like some kind of tag helpers with a lambda but I cant get them to work either.

Blazor - An Introduction - from youtube

I return a list of survey objects and foreach over them and create a table. Id like to be able to click on the row (button or something) and query up the details of the selected survey and its questions. I supposed I could do this by just routing to a new page with a href but for fun Id like to figure this out.

            @foreach (var survey in surveys)
            {
            <tr>
                <td><button class="btn btn-primary" onclick="@GetSurvey(survey.Id)" > @survey.Name</button></td>
                <td>@survey.Active</td>
            </tr>
            }

I have an task where I'd like to use the id passed in to then go query the api and return some related data.

    async Task GetSurvey(int id)
    {
        // then query server api with this ID
    }
davidlsharp1 commented 6 years ago

Nevermind. I had unnecessary quotes.

This was covered here:

https://github.com/aspnet/Blazor/issues/923

onclick=@(() => GetSurvey(survey.Id))

Andrzej-W commented 6 years ago

@davidlsharp1 Strictly speaking problem is not related to quotes. If you want to pass your own parameters you have to use lambda expression and that is real problem in original post. You can write: onclick="@(() => GetSurvey(survey.Id))" and it will work Some examples are available here: https://blazor.net/docs/components/index.html#event-handling

davidlsharp1 commented 6 years ago

Thanks @Andrzej-W I appreciate the help.

horseyhorsey commented 5 years ago

@davidlsharp1

I just came across similar issue populating a table with button.

If you don't use a lambda then the "onclicks" are invoked when the table is populated.