Displaying graphic dynamically - from path as content of a field

Dean Everhart 1,516 Reputation points
2024-07-14T15:36:56.13+00:00

Perhaps someone could help me with the ahref / src format to display a graphic from the wwwroot folder from a path that is content of a field?

Trial and Error Attempts:

User's image

Environment:
User's image

User's image

User's image

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,360 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,603 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,416 Reputation points
    2024-07-14T19:51:38.14+00:00

    You can use Url.Content to ensure that the ~ resolves to your wwwroot folder:

    <img src="@Url.Content(Model.Graphic)" />
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 27,581 Reputation points
    2024-07-14T19:51:06.8233333+00:00

    I think the code sample below is close to what you are trying to do.

    Model

        public class ImageVm
        {
            public int Id { get; set; }
            public string Name { get; set; } = string.Empty;
            public string Graphic { get; set; } = string.Empty;
            public string Website { get; set; } = string.Empty;
        }
    
    

    Controller

    public class ImagesController : Controller
    {
        private IWebHostEnvironment _env;
        public ImagesController(IWebHostEnvironment env)
        {
            
            _env = env;
        }
        public IActionResult Index()
        {
            List<ImageVm> model = PopulateDemoData();
            return View(model);
        }
    
        public List<ImageVm> PopulateDemoData()
        {
            return new List<ImageVm>
            {
                new ImageVm { Id = 1, Graphic="guitar_PNG3338.png", Name="Image_1", Website="/Home/Index" },
                new ImageVm { Id = 1, Graphic="guitar_PNG3362.png", Name="Image_1", Website="/Forms/Index" },
                new ImageVm { Id = 1, Graphic="guitar_PNG3375.png", Name="Image_1", Website="/Images/Index" },
            };
        }    
    }
    
    

    View

    @model List<MvcDemo.Models.ImageVm>
    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    <div>
    @foreach(var item in Model) {
        <div>
            <a href="@item.Website">
                <img src="/images/@item.Graphic" />
            </a>
        </div>
    }
    </div>
    
    

    When asking for help, it is a lot easier for the community if you post sample code not screenshots. Also you have a property named GraphicIconAccess but the model does not have this property which makes it difficult to trust the code.

    0 comments No comments