Search code examples
c#asp.net-core-mvcblazor

Can I inject controller from ASP.NET Core MVC architecture to Blazor components?


We have a project based on ASP.NET Core MVC. We started to use some additional filters and pages with Blazor.

Now I have a problem redirecting or navigating to another view (.cshtml) page after clicking on the button.

I tried many things but still can't find how to solve this problem.

I'm getting id from the context.Item.ID but it never hit the controller site to show edit view.

How should I re-work this controller call / or controller method to show another Edit-view page to return the edit view with the correct id?

Thanks a lot!

@inject UploadController _controller
.
.
.

<MudIconButton Size="@Size.Small" Icon="@Icons.Material.Outlined.Edit" OnClick="(()=>EditDoc(context.Item.ID))" />

protected async void EditDoc(int id)
{
    await _controller.DocumentEdit(id);
}

Solution

  • Basically what I found is that I need to use NavigationManager with the method NavigetTo with the parameter forceLoad = true this is important. The code looks like this:

    protected async Task EditDoc(int id)
    {
        NavManager.NavigateTo($"/Upload/DocumentEdit/{id}", true);      
    }
    

    Calling Upload Controller / DocumentEdit method and hereafter thinks what I need just return View();