'Access razor component property from within a JavaScript event

A razor component binds its OnInput event to a JavaScript function. How can I read the razor component property from within the JavaScript function?



Solution 1:[1]

You can add a method in your Razor component with the [JSInvokable] attribute. In this method you can return the properties that you need in your Javascript.

[JSInvokable]
public static Task<int[]> ReturnArrayAsync()
{
    return Task.FromResult(this.MyProperty);
}

And in javascript side:

DotNet.invokeMethodAsync('{ASSEMBLY NAME}', 'ReturnArrayAsync', {ARGUMENTS}); //in that case we do not need arguments

It should do the trick. Good luck :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dylan Barquilla