Gill
Gill

Reputation: 37

Blazor App Razor Pages Routing With parameters

trying to Use hyperlink column and performing routing on click

UriHelper.NavigateTo($"{ Employee.Link}/{Employee.EmployeeID.ToString()}/{Employee.Name}/{ Employee.Title}");

Not sure what to name the Razor page which will be retrieve using the dynamic link and how to initialize it with the required details

@page "/???????/{EmpID}/{Name}/{Title}"

it works when I have razor page named as Employee.link and initializing it with the Employee.link details

@page "/sara_clarck/{EmpID}/{Name}/{Title}"

but I do not want to create one razor page for each employee

Upvotes: 0

Views: 120

Answers (1)

Bennyboy1973
Bennyboy1973

Reputation: 4246

The only thing you should need to pass to a new page is an ID. Your receiving page, let's call it /employees/{EmpID}, should be able to look up the employee's name and title on its own, probably in the OnInitializedAsync override.

So something like:

EmployeeViewer.razor

@page "/employees/{EmpID:int}"

@if (DisplayEmp is not null)
{
    <SomeComponentThatDisplaysEmployeeInfo Employee=DisplayEmp />
}

@code {
    [Parameter]
    public int EmpID { get; set; }

    public Employee? DisplayEmp = null;

    protected override void OnInitialized()
    {
        DisplayEmp = MyDbService.GetEmployee(EmpID);
    }
}

As Shaun says, you can name the actual file whatever you want, as you specify the component's path as a page. A component can even have multiple routes defined, e.g. your main page could be @page "/" and also (on another line) @page "/index"

Upvotes: 3

Related Questions