I'm encountering an issue where I cannot pass arguments to a C# method from JS in Blazor.
Here is the code snippet causing the problem:
<button class="m-2 btn btn-secondary" @onclick="FindMultiplication">Show Sum</button>
public async Task FindMultiplication()
{
await JSRuntime.InvokeVoidAsync("CallCalculateMultiplication", DotNetObjectReference.Create(this), 1, 2);
}
[JSInvokable]
public int CalculateMultiplication(int no1, int no2)
{
int multiplication = no1 * no2;
return multiplication;
}
JS:
function CallCalculateMultiplication(obj, no1, no2)
{
var valueR = obj.invokeMethodAsync("CalculateMultiplication", no1, no2);
}
The issue arises when the CalculateMultiplication
method of the Blazor Component is not being called.
I have observed that if no arguments are passed to the CalculateMultiplication
method, it gets called. The modified code below functions correctly:
[JSInvokable]
public int CalculateMultiplication()
{
//int multiplication = no1 * no2;
return 2;
}
function CallCalculateMultiplication(obj, no1, no2)
{
var valueR = obj.invokeMethodAsync("CalculateMultiplication");
}
Why is this happening? This used to work in Blazor 3.1 as shown in this YouTube Video (check after 10:00 mark), but now it does not. My question is how should I send arguments to instantiate a razor component C# method?