question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Blazor client onclick function with parameter

See original GitHub issue

Adding parameter to a function in Blazor client throws compiler error: Cannot convert from void to string, example:

<a onclick="@MyFunction">My function</a>

void MyFunction() {
    Console.WriteLine("Works");
}


<a onclick="@MyFunction(1)">My function</a>

void MyFunction(int number) {
    Console.WriteLine("Throws error");
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Andrzej-Wcommented, Jun 22, 2019

@Madunet add @ before onclick

<button class="btn btn-primary" @onclick="@(() => test(a))">Set counter to @a</button>
0reactions
Paul-Schroedercommented, Sep 20, 2019

I needed similar syntax for invoking a method with a parameter and here is what worked for me:

Razor: <button class="btn btn-primary" onclick="@((Action<EventArgs>) (args => RegisterClicked(args, ConferenceEvent.ConferenceEventId)))">Register</button>

Code: private void RegisterClicked(EventArgs args, Guid conferenceEventId) { System.Diagnostics.Debug.WriteLine($"Register for {conferenceEventId}"); }

Note: I’m using RC1, which includes some changes necessary as of Preview 9:

“Replace Microsoft.AspNetCore.Components.UIEventArgs with System.EventArgs and remove the “UI” prefix from all EventArgs derived”

See: https://devblogs.microsoft.com/aspnet/asp-net-core-and-blazor-updates-in-net-core-3-0-preview-9/

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Pass Arguments to Your onclick Functions in Blazor
Blazor enables you to handle HTML events such as onClick events via event handlers. You can use lambdas to capture values (for example...
Read more >
Blazor how to pass arguments to onclick function?
Try it with a lambda. You're binding the onclick to the result of the function rather than the function itself. @for (int i...
Read more >
How do I pass arguments to the onclick event in Blazor?
You can use a lambda expression to pass multiple arguments to an onclick event. @for(var i=0; i<5; i++) { var index = i;...
Read more >
ASP.NET Core Blazor event handling
Learn about Blazor's event handling features, including event argument types, event callbacks, and managing default browser events.
Read more >
[Solved]-Blazor how to pass arguments to onclick function?-C#
Try it with a lambda. You're binding the onclick to the result of the function rather than the function itself. ... The following...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found