Blazor: Invoking C# static method from JS function called from HTML onclick--why can't the JS find the method?

MadBanana

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.

Ericgrantholland

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');
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

In Blazor, how can I call a JS function from within a static C# method?

From Dev

vue.js / bootstrap: onclick event method not called, error "Can't find variable: updateDocument"

From Dev

Use injected service from JS code to static Blazor method

From Dev

Calling a non static method using blazor interop from js

From Dev

Can't find method from User.js

From Dev

why js can't run method as a function

From Dev

How to call C# none static method in Blazor component from JS file with parameter?

From Dev

iOS Why can't the method collectionView:didSelectItemAtIndexPath: be called from UITapGestureRecognizer?

From Dev

Why isn't this method getting called from my Rails unobtrusive js template

From Dev

How to invoke JS async method from Blazor

From Java

Why can't I use from the static method of the implemented interface?

From Dev

Why can't I call a static method from the constructor?

From Dev

Can not verify a method which is called from a static method

From Dev

Invoking a JS method exposed by Flex app from a secondary window

From Dev

method can't be called only from AJAX

From Dev

Class Scope with method called from subclass JS

From Dev

Why find() method doesn't show the recent data from mongoDb database in Express.Js

From Javascript

js call static method from class

From Dev

Invoking A Method From Another File C#

From Dev

How can I call a method from html attribute in vue js

From Dev

Call a C# non static method from a static method in Blazor invoked by Javascript DotNet.invokeMethodAsync

From

Why I'm getting 'Non-static method should not be called statically' when invoking a method in a Eloquent model?

From Java

Android Recycler View problem in "onBindViewHolder" function "static method cannot be called from non-static method"

From Dev

Can't pass data from model to method in Vue.js

From Dev

non static method can't be called statically

From Dev

Call Blazor method from JS without button click

From Dev

Calling Method from JS Dotnet Maui Blazor App

From Dev

Blazor page with child component: How can I pass current HTML table row index or Vairable from the row @onclick method button

From Dev

How do I write a Java method, to be called from server side JavaScript, that takes a JS callback function as an argument?

Related Related

  1. 1

    In Blazor, how can I call a JS function from within a static C# method?

  2. 2

    vue.js / bootstrap: onclick event method not called, error "Can't find variable: updateDocument"

  3. 3

    Use injected service from JS code to static Blazor method

  4. 4

    Calling a non static method using blazor interop from js

  5. 5

    Can't find method from User.js

  6. 6

    why js can't run method as a function

  7. 7

    How to call C# none static method in Blazor component from JS file with parameter?

  8. 8

    iOS Why can't the method collectionView:didSelectItemAtIndexPath: be called from UITapGestureRecognizer?

  9. 9

    Why isn't this method getting called from my Rails unobtrusive js template

  10. 10

    How to invoke JS async method from Blazor

  11. 11

    Why can't I use from the static method of the implemented interface?

  12. 12

    Why can't I call a static method from the constructor?

  13. 13

    Can not verify a method which is called from a static method

  14. 14

    Invoking a JS method exposed by Flex app from a secondary window

  15. 15

    method can't be called only from AJAX

  16. 16

    Class Scope with method called from subclass JS

  17. 17

    Why find() method doesn't show the recent data from mongoDb database in Express.Js

  18. 18

    js call static method from class

  19. 19

    Invoking A Method From Another File C#

  20. 20

    How can I call a method from html attribute in vue js

  21. 21

    Call a C# non static method from a static method in Blazor invoked by Javascript DotNet.invokeMethodAsync

  22. 22

    Why I'm getting 'Non-static method should not be called statically' when invoking a method in a Eloquent model?

  23. 23

    Android Recycler View problem in "onBindViewHolder" function "static method cannot be called from non-static method"

  24. 24

    Can't pass data from model to method in Vue.js

  25. 25

    non static method can't be called statically

  26. 26

    Call Blazor method from JS without button click

  27. 27

    Calling Method from JS Dotnet Maui Blazor App

  28. 28

    Blazor page with child component: How can I pass current HTML table row index or Vairable from the row @onclick method button

  29. 29

    How do I write a Java method, to be called from server side JavaScript, that takes a JS callback function as an argument?

HotTag

Archive