Add the .NET function invoke statement to a script in the head of Pages/_Host.cshtml file.
<script>
function CSMethod() {
DotNet.invokeMethodAsync('BlazorTestApp', 'CSCallBackMethod');
}
</script>
Here we are defining a JavaScript function “CSMethod”. This function will have a callback to our .NET function “CSCallBackMethod” which is defined in index.razor.
To invoke C# method from JavaScript,
The method must be decorated with “JSInvokable” attribute.
The method must be public.
The method may either be static or instance-level (this is only supported by Blazor 0.5.0 and above).
There are two methods to call a method from JavaScript:
The syntax of calling a C# method from JavaScript is as follows.
Add the .NET function invoke statement to a script in the head of Pages/_Host.cshtml file.
Here we are defining a JavaScript function “CSMethod”. This function will have a callback to our .NET function “CSCallBackMethod” which is defined in index.razor.
To invoke C# method from JavaScript,
- The method must be decorated with “JSInvokable” attribute.
- The method must be public.
- The method may either be static or instance-level (this is only supported by Blazor 0.5.0 and above).
- The Identifier for the method must be unique.
- The method must be nongeneric.
need an explanation for this answer? contact us directly to get an explanation for this answer