How to set Click on picture then go to new different page.That mean one picture one new page.

MIPAKTEH_1 240 Reputation points
2024-06-16T04:17:09.63+00:00
@page "/"
@rendermode InteractiveServer
@inject NavigationManager Navigation

<PageTitle>Home</PageTitle>

<h1>Hello, world!</h1>

Welcome to your new app.
<p></p>
    @foreach (var image in images)
    {
        <div style="margin:10px 20px; float:left;">
        <img src=@($"/Images/{image}") asp-append-version="true" : style="width=100px;height:100px" @onclick="() => NavigateToNewPage()" />
        </div>
    }
@code {
    // Define the list of image file names
    private readonly List<string> images = new()
    {
        "A.png",
        "B.jfif",
        "C.jfif",
        "D.jfif"
    };

    private void NavigateToNewPage()
    {
        Navigation.NavigateTo("/A");
        Navigation.NavigateTo("/B");
        Navigation.NavigateTo("/C");
        Navigation.NavigateTo("/D");

    }
}



Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,470 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ping Ni-MSFT 3,035 Reputation points Microsoft Vendor
    2024-06-17T02:02:19.78+00:00

    Hi @MIPAKTEH_1,

    Here is a whole working demo you could follow:

    @page "/"
    @rendermode InteractiveServer
    @inject NavigationManager Navigation
    <PageTitle>Home</PageTitle>
    <h1>Hello, world!</h1>
    Welcome to your new app.
    <p></p>
    @foreach (var image in images)
    {
        <div style="margin:10px 20px; float:left;">
            <img src=@($"/Images/{image}") asp-append-version="true" style="width:100px;height:100px" @onclick="() => NavigateToNewPage(image)" />
        </div>
    }
    @code {
        // Define the list of image file names
        private readonly List<string> images = new()
        {
            "A.png",
            "B.jfif",
            "C.jfif",
            "D.jfif"
        };
        // Define a dictionary to map image names to their corresponding URLs
        private readonly Dictionary<string, string> imageToPageMap = new()
        {
            { "A.png", "/PageA" },
            { "B.jfif", "/PageB" },
            { "C.jfif", "/PageC" },
            { "D.jfif", "/PageD" }
        };
        private void NavigateToNewPage(string imageName)
        {
            if (imageToPageMap.TryGetValue(imageName, out var url))
            {
                Navigation.NavigateTo(url);
            }
        }
    }
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Rena

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful