Skip to content
Advertisement

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

I understand we can change a C# property value in Blazor from JavaScript by invoking the method DotNet.invokeMethodAsync. I have the below working but in this method I want to also call a non static method.

JS File:

JavaScript

Razor page:

JavaScript

I am trying to run a non static method in a static method (IN BLAZOR APPLICATION). How can I call this method?

I attempted to make the following method static and got the below error:

JavaScript

CS0120: An object reference is required for the nonstatic field, method, or property ‘JSRuntime’

How do I make this static: @inject IJSRuntime JSRuntime

Advertisement

Answer

That’s actually described in the docs

summery: create a static Action for the update and register the local instance

JavaScript

(not tested)

NOTE It is important to remember that you can still invoke the JSInvokable method even if the component is not loaded (i.e. no instance). This might infer undefined behavior!

edit: changed to async Task, due to the async InvokeVoidAsync method

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement