Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a parameter to razor component in server-side Blazor?

How can I pass parameter into razor component?

So far I tried

@(await Html.RenderComponentAsync<Rateplan>(RenderMode.ServerPrerendered, new { id= 100}))

But I receive an error

InvalidOperationException: Prerendering server components with parameters is not supported.

I try the same thing with RenderMode.ServerPrerendered but I receive an error

InvalidOperationException: Server components with parameters are not supported.

I also tried doing

<Rateplan Id="100"></Rateplan>

but that didnt even start the component.

like image 898
mko Avatar asked Sep 25 '19 07:09

mko


People also ask

How do you pass parameters in Razor component?

Begin by creating a new Blazor Server App project in Visual Studio. Then right click on the Pages folder and select Add New Item menu option. Add a new Razor Component named Message. razor to the Pages folder.

How do you pass multiple parameters to Blazor component?

The easiest way is to use Route parameters instead of QueryString: @page "/navigatetopage/{id:int}/{key}" @code { [Parameter] public int Id{get;set;} [Parameter] public string Key{get;set;} ... }


3 Answers

Set RenderMode as Static

@(await Html.RenderComponentAsync<Rateplan>(RenderMode.Static, new { id = 100 }))
like image 30
Alexey Belanov Avatar answered Oct 03 '22 00:10

Alexey Belanov


In the component where you want to accept the parameter you need to have a Property marked as a parameter

Like

[Parameter]
public List<Player> Players { get; set; }

Then you should be able to pass in the parameter as

<Componentname Players="@players"></Componentname>

(In this example @players is a local variable)

like image 147
anders stubberup Avatar answered Oct 01 '22 00:10

anders stubberup


The problem and workaround is described in this article. (There is a little bug, because GetModel should be named GetCustomerId) Passing parameters is not supported, exactly as the Exception says.

You can wait for ASP.NET Core 3.1, where the ability to pass parameters will be restored.

I have implemented the solution from the first article for parameter OperationId like this - razor component's code:

using Microsoft.JSInterop;

[Inject]
protected IJSRuntime jrt { get; set; }

protected async override Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender)
    {
        try
        {
            var oid = await jrt.InvokeAsync<string>("GetOperationId");
            int opid;
            if (int.TryParse(oid, out opid))
                OperationId = opid;
        }
        catch (Exception ex)
        {
            ls?.LogException(ex, "Sortiment.OnAfterRenderAsync");
        }
        //This code was moved from OnInitializedAsync which executes without parameter value
        if (OperationId.HasValue)
            sortiment = await ProductService.GetSortimentAsync(null, OperationId, Odpady);
        else
            productFilter = await ProductService.GetProductFilterAsync();
        StateHasChanged(); //Necessary, because the StateHasChanged does not fire automatically here
    }
}

and added this to the hosting Razor page:

@section Header
{
  <script>
  function GetOperationId() {
    return "@Model.OperationId";
  }
  </script>
}

This workaround works only for RenderMode.Server.

like image 2
Vojtěch Dohnal Avatar answered Oct 02 '22 00:10

Vojtěch Dohnal