I have Image in blazor page then need a comment for each of Image.Can somebody show me how to do.

MIPAKTEH_1 365 Reputation points
2024-07-10T14:51:27.4966667+00:00
@page "/"

@rendermode InteractiveServer
@inject NavigationManager Navigation
<h1>Home</h1>
<PageTitle>Home</PageTitle>
<h1 style="color: blue;">Promosi Barang Perniagaan Anda!!</h1>
Welcome to your new app.
<p> </p>

@for (int i = 0; i < images.Count; ++i)
{

    string tempImageName = images[i].Page;
    string tempTitle = images[i].Title;
    <div style="display:inline-block; text-align:center; margin:10px;">
        <img src=@($"/Images/{images[i].Image}")
             style="width:200px; cursor:pointer"
             @onclick="() => NavigateToNewPage(tempImageName)" />
        <div>@tempTitle</div>
        @if (true)
        {

            @($"{""}...")
        }
    </div>
}

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

Accepted answer
  1. Manman Xie - MSFT 165 Reputation points Microsoft Vendor
    2024-07-11T08:36:15.9466667+00:00

    Hi @MIPAKTEH_1 ,

    You can create a field to control whether you can comment or not.Then judge whether to display the comment module in the page

    Here's an example:

    @page "/image-demo"
    @rendermode InteractiveServer
    <h1>Home</h1>
    <PageTitle>Home</PageTitle>
    <h1 style="color: blue;">Promosi Barang Perniagaan Anda!!</h1>
    Welcome to your new app.
    <p> </p>
    @for (int i = 0; i < images.Count; ++i)
    {
        string tempImageName = images[i].Page;
        string tempTitle = images[i].Title;
        int buttonNumber = i;
        <div style="float:left; text-align:center; margin:10px;">
            <img src=@($"/Images/{images[i].Image}")
                 style="width:200px;height:200px; cursor:pointer;border:dashed 1px #333;" />
            <div>@tempTitle</div>
            <div style="width:200px;height:50px;">
                @if (images[i].CanComment)
                {
                    <input style="width:60%" />
                    <button class="btn-primary small" @onclick="@(e => ChangeStatus(e, buttonNumber))">comment</button>
                }
               
            </div> 
        </div>
       
    }
    @code{
        public List<Images> images { get; set; }
        public class Images
        {
            public string Page { get; set; }
            public string Title { get; set; }
            public string Image { get; set; }
            public bool CanComment { get; set; }
        }
        protected override void OnInitialized()
        {
            images = new List<Images>
            {
              new() { Page="11", Title="1", Image="", CanComment=true },
              new() { Page="22", Title="22", Image="", CanComment=true },
              new() { Page="33", Title="33", Image="", CanComment=true }
            };
        }
        private void ChangeStatus(MouseEventArgs e, int buttonNumber)
        {
            // You can save the comment in the table
            images[buttonNumber].CanComment = false;
        }
    }
    
    

    The result is  as follows

    undefined

    You can put  a comment button ,click it open a comment dialog

    <div style="width:200px;height:50px;">
        @if (images[i].CanComment)
        {
            <button class="btn-primary small" onclick="alert('open comment dialog')">comment</button>
        }
    </div>
    

    The result is  as follows

    image

     

    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,

    Manman Xie

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.