Home > Net >  How can I create a List of Razor Page in Blazor
How can I create a List of Razor Page in Blazor

Time:11-13

I'm trying to create a list of razor page.
I explain myself, in my "Index.razor", I actually have that :

@page "/"
@inject IJSRuntime JSRuntime

<h1 style="text-align:center;color:red" >Hey :)</h1>
<br />
<Counter/>
<NewCounter/>
<AnotherVersion/>

Who redirect to Counter.razor, NewCounter.razor & AnotherVersion.razor.

but, like a string list I want to create something like that : having a list like that :

List<Page> myPage = new List<Page>{Counter, NewCounter, AnotherVersion};
@page "/"
@inject IJSRuntime JSRuntime

<h1 style="text-align:center;color:red" >Hey :)</h1>
<br />
@for(int i = 0; i < myPage.Count; i  )
{
    myPage[i];
}

CodePudding user response:

@page "/"

@foreach (string pageName in pageNames)
{    
   <ul>
      <li><a href="@pageName">@pageName</a></li>
   </ul>
}

@code {

   private List<string> pageNames = new List<string> 
   { 
      "Counter", 
      "NewCounter", 
      "AnotherVersion" 
   };

}

CodePudding user response:

Okay someone post the answer and delete it right after.
So, here the answer :

@page "/"
@inject IJSRuntime JSRuntime

<h1 style="text-align:center;color:@bgColor" >My Dashboard</h1>
<br />
@foreach (var component in widget)
{
    <DynamicComponent Type=@component />
}

@code {
    public System.Type[] widget = new[] { typeof(Counter), typeof(NewCounter), typeof(AnotherVersion) };
}

(If he repost his message, I will delete mine)

  • Related