How to change code "Foreach" to Loop.

MIPAKTEH_1 260 Reputation points
2024-06-28T14:53:36.5233333+00:00
@page "/"

@rendermode InteractiveServer
@inject NavigationManager Navigation


<h1 style="color: blue;">Anda!!</h1>
<p></p>

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()
    {
        "house_.png", "house_1.png", "house_2.png"
    };

    private readonly List<string> titles = new()
    {
       "Title 1", "Title 2", "Title 3"
    };

    private readonly Dictionary<string, string> imageToPageMap = new()
    {
        {  "house_.png", "/PageA" },
        { "house_1.png", "/PageB" },
        { "house_2.png", "/PageC" }
    };

    // Method to navigate to the new page
    private void NavigateToNewPage(string imageName)
    {
        if (imageToPageMap.TryGetValue(imageName, out var url))
        {
            Navigation.NavigateTo(url);
        }
    }
}
want to change

@for (int i = 1; i <= images.Count; ++i)
{
    <img src=@($"/Images/H_{i}.jfif")
         style="left:auto; margin-left:10px; width:200px; cursor:pointer"
         @onclick="() => NavigateToNewPage(i)" />
    @if ((i % 3) == 0)
    {
        <br />
    }
}


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
{count} votes

Accepted answer
  1. AgaveJoe 27,341 Reputation points
    2024-06-29T12:18:17.4666667+00:00

    Sorry, I must have copied the wrong code to the forum. You have to get the state of the "Page" value into a variable because "i" is 3 when the loops ends.

    @for (int i = 0; i < images.Count; ++i)
    {
        string tempImageName = images[i].Page;
        <img src=@($"/Images/{images[i].Image}")
             style="left:auto; margin-left:10px; width:200px; cursor:pointer"
             @onclick="() => NavigateToNewPage(tempImageName)" />
    
    }
    

    Title not shown and click event on picture not to the page.

    There's no "title" in your original code. I'm not sure where "title" should be located. If it is the title attribute of the img element then simply add the attribute like you're doing with the src attribute.

    @for (int i = 0; i < images.Count; ++i)
    {
        string tempImageName = images[i].Page;
        <img src=@($"/Images/{images[i].Image}")
             style="left:auto; margin-left:10px; width:200px; cursor:pointer"
             title=@($"{images[i].Title}")
             @onclick="() => NavigateToNewPage(tempImageName)" />
    
    }
    
    
    

    To deal with the three images per line within the for...loop.

        int tempI = i;
        @if (((tempI + 1) % 3) == 0)
        {
            <br />
        }
    
    

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 27,341 Reputation points
    2024-06-28T15:53:46.98+00:00

    I think this is the approach you are looking for.

    <h1 style="color: blue;">Anda!!</h1>
    <p></p>
    
    Welcome to your new app.
    
    <p></p>
    //Updated 
    @for (int i = 0; i < images.Count; ++i)
    {
        string tempImageName = images[i].Page;
        <img src=@($"/Images/{images[i].Image}")
             style="left:auto; margin-left:10px; width:200px; cursor:pointer"
             title=@($"{images[i].Title}")
             @onclick="() => NavigateToNewPage(tempImageName)" />
        int tempI = i;
        @if (((tempI + 1) % 3) == 0)
        {
            <br />
        }
    }
    
    @code {
        public class MyViewModel
        {
            public string Image { get; set; } = string.Empty;
            public string Title { get; set; } = string.Empty;
            public string Page { get; set; } = string.Empty;
    
        }
    
        private readonly List<MyViewModel> images = new List<MyViewModel>()
        {
            new MyViewModel()
            {
                Image = "guitar_PNG3338.png",
                Title = "Title 1",
                Page = "/PageA"
            },
             new MyViewModel()
            {
                Image = "guitar_PNG3362.png",
                Title = "Title 2",
                Page = "/PageB"
            },
             new MyViewModel()
            {
                Image = "guitar_PNG3375.png",
                Title = "Title 3",
                Page = "/PageC"
            },
        };
    
        // Method to navigate to the new page
        private void NavigateToNewPage(string page)
        {
            Console.WriteLine(page);
            Navigation.NavigateTo(page);
        }
    
    }
    
    
    1 person found this answer helpful.