Blazor client onclick function with parameter
See original GitHub issueAdding 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:
- Created 4 years ago
- Comments:8 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@Madunet add
@beforeonclickI 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/