0
votes

At the moment i am building a ASP.NET app with blazor woven in. I can render my blazor components with the Html.RenderComponentAsync<MyComponent>() function included.

The problem is, is that i want to be able to get event from this components so i can react to them outside of the component itself.

Usually in blazor you would add this to your child component code section like this,

    [Parameter] public Action<string> addPressed {get; set;}

and then handle it in the parent component like so

<MyComponent addPressed="DoSomethingOnPressed"/>

How would i got about that when my component was instead instantiated through RenderComponentAsync like this

@(await Html.RenderComponentAsync<MyComponent>())

Thanks in advance

1
Do you want to react to event on the server-side ? or inside the browser? - codevision
@codevision preferably on the server side as i will be rerendering components - duck
I don't know if this works, but you can pass parameters to the component, have you tried something like @(await Html.RenderComponentAsync<MyComponent>( addPressed = DoSomethingOnPressed)) - Mister Magoo

1 Answers

0
votes

Just use overload which allow you to pass Model object which will be mapped to Properties of the Blazor application.

@(await Html.RenderComponentAsync<MyComponent>(new { addPressed = (_) => Console.WriteLine(_) }))

Actual method may be constructed in _Host.cshtml page, or injected via services.

Thanks Mister Magoo for the hint.