Home > Enterprise >  How to use NavigationManager.NavigateTo in a NavLink or anchor?
How to use NavigationManager.NavigateTo in a NavLink or anchor?

Time:08-31

I have the following in code:

navigationManager.NavigateTo(
    $"Controller/Action?value1={value1}&value2={value2}",
    forceLoad: true);

I changed the page to something like:

<p>
    @foreach (var item in items)
    {
        <div >
            <div >
                @item.Description
            </div>
        </div>
    }
</p>

What I need is to enclose @item.Description in a NavLink or anchor which would do the same as the NavigateTo. It is import that forceLoad is done in order to refresh the page.

How can I do this?

CodePudding user response:

Simply add an anchor and bind the click event

@inject NavigationManager navigationManager

<p>
    @foreach (var item in items)
    {
        <div >
            <div >
                <a href="#" @onclick="Navigate" @onclick:preventDefault>@item.Description</a>
            </div>
        </div>
    }
</p>

@code {
    private void Navigate()
    {
        navigationManager.NavigateTo(
            $"Controller/Action?value1={value1}&value2={value2}",
            forceLoad: true);
    }
}
  • Related