0

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;
    }
}
1
  • Refactor your code by extracting you method in a service you share between page Commented Jun 23, 2020 at 7:57

1 Answer 1

2
<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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.