Reputation: 35
Inside a Blazor component, I want to call a JS method from the HTML onclick event of an anchor tag, thus:
<a onclick="window.LoadImage();" class="@CssClass">@DisplayName</a>
My JS method is:
window.LoadImage = () => {
DotNet.invokeMethodAsync('MyProject', 'FireEvent').then();
}
Inside the JS method, I want to fire a C# static method to trigger an event, thus:
[JSInvokable]
public static void FireEvent()
{
StateEngine.LoadImage(DisplayName, Target);
}
[Parameter]
public string DisplayName { get; set; }
[Parameter]
public string Target { get; set; }
I can't get the JS method to find the C# method. I've tried multiple name configurations for the first parameter, like
MyProject (the assembly name)
MyProject.FolderName.ComponentName
FolderName.ComponentName
ComponentName
to name a few. I've also tried shuffling names through the second parameter.
How is this supposed to work? What am I missing?
P.S. Please disregard the bit about the instance properties in the static method. I've got a workaround for that, but don't want to clutter up the post.
Upvotes: 3
Views: 619
Reputation: 197
From the looks of it, you would be better off using the reference JSInterop so you can access the components properties.
Razor
@inject IJSRuntime JS
@implements IDisposable
<a @onclick=Clicked>Click here</a>
@code
{
private DotNetObjecctReference ObjRef;
[JSInvokable]
public void FireEvent()
{
StateEngine.LoadImage(DisplayName, Target);
}
private Task Clicked()
{
return JS.InvokeVoidAsync("window.LoadImage", ObjRef);
}
protected override Task OnInitializedAsync()
{
ObjRef = DotNetObjectReference.Create(this);
}
void IDisposable.Dispose()
{
ObjRef.Dispose();
}
}
JS
window.LoadImage = (dotNetObject) => {
dotNetObject.invokeMethod('FireEvent');
}
Upvotes: 1
Reputation: 1163
Your code looks good. May be its unable to find your .Net method. Give it a retry..
JS
window.LoadImage = function () {
DotNet.invokeMethodAsync("MyBlazorProject.UI", "FireEvent").then();}
First Parameter must be AssemblyName of your Blazor component. In my case, it is "MyBlazorProject.UI".
Second Parameter must be method name.
HTML
<a onclick="window.LoadImage()" class="btn btn-primary">Call .Net Method FireEvent</a>
here onclick is JavaScript event, which is good in your case too.
C# Code
[JSInvokable]
public static void FireEvent()
{
//debugger hits here
//StateEngine.LoadImage(DisplayName, Target);
}
This is all working for me fine. Debugger gets hit inside FireEvent .Net method after clicking anchor tag.
Thanks
Upvotes: 0