Sabish Sasi
Sabish Sasi

Reputation: 1

Access Method in a Layout component

I have a method with 3 parameters in the layout. How can I call the function in the load event of a page which is using the layout.

@inherits LayoutComponentBase
<div class="sidebar">
    <NavMenu />
</div>
<div class="main">
    <div class="top-row px-4">
        <a href="https://learn.microsoft.com/aspnet/" target="_blank">About</a>
    </div>

    <div class="content px-4">
        @Body
    </div>
</div>
@code {

    private string x;
    public void Test (string a , string b, string c)
    {
        x = a + b + c;
    }
}

Upvotes: 0

Views: 810

Answers (1)

Brian Parker
Brian Parker

Reputation: 14583

<CascadingValue Value="this" Name="MainLayout">
    @Body
</ CascadingValue>

then in your component

@code {
    [CascadingParameter(Name = "MainLayout")]
    public MainLayout MainLayout { get; set; }

    void Test (string a , string b, string c) => MainLayout.Test(a, b, c);
}

Here is an example component where I am doing something similar.

Upvotes: 2

Related Questions