1

I need some help on how to architecture a Blazor application to achieve what I need.

All the pages for this application must have the same starting parameter, say AccountId. So a product page could be like "/product/{AccountId}", customer page could be like "/customer/{AccountId}". This AccountId value is used to retrieve basic account information like name, phone.

The site shares global nav bars, here is what the MainLayout looks like:

<TopNav></TopNav>
<LeftNav></LeftNav>
<main class="app-main">
<div class="wrapper">
    <div class="page">
        <div class="page-inner">
            @Body
        </div>
    </div>
</div>

What I need is to display the account name in the TopNav layout component. I've tried to create a BasePage class that they all inherit from and tried to fetch the account info in the OnInitializedAsync() of the BasePage and set a protected variable. However, the TopNav component does not receive this value and always see it as null.

Just how should I structure my application so that both the individual page component, and all the layout component receives this same account info object?

Thanks a lot!

2
  • which blazor, server or client side ? Commented Apr 30, 2020 at 15:16
  • I am using server-side. Commented Apr 30, 2020 at 16:33

2 Answers 2

1

Suppose you've created a Company parent component which can get a route parameter to represent a company ID, the value of which is used to retrieve the company's details that may be displayed in a child component. You may define the parent component like this:

Company.razor

@page "/company"
@page "/company/{ID}"

@code {
    [Parameter]
    public string ID { get; set; }
}

Note: The two route templates above are necessary...

Answering your question... Add this code to the MainLayout component

@code{

    public string ID { get; set; }

    protected override void OnParametersSet()
    {
        // pull out the "ID" parameter from the route data
        object id = null;
        if ((this.Body.Target as RouteView)?.RouteData.RouteValues?.TryGetValue("ID", out id) == true)
        {
            ID = id?.ToString();
        }

    }
}

Note: The code above extricates the value of the ID parameter from the RouteData, and stores it in the ID property. Now you can do with it, whatever you want... including passing it to the the NavMenu Component if it's necessary there. This is how you can do it:

Add a Component Parameter property to the NavMenu Component, like this:

[Parameter]
public string ID { get; set; }

And add an ID component parameter attribute to the NavMenu component instance (located at the top of the MainLayout component. It should be now <NavMenu ID="@ID"/>

Note: This code sample describes how you can extricate the parameter ID (AccountID, etc.) within MainLayout and how to pass it to its child component, NavMenu ( TopNav, LeftNav). Now that you've got the ID, you can pass it to any data store you've defined, and retrieve some data details...

Hope this helps...

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

1 Comment

Maybe a cascading parameter might be a simpler solution
0
  1. Create a service getting and setting the current account.
  2. Bind the NavBar on this service current account properties

2 Comments

Thanks for the tip, but how does this service gain access to the routing parameters?
You can either subscribe to navigation changes, or set the new account on each pages or in a component getting the url parameter as binded parameter

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.