ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,777 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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:
Environment:
You can use Url.Content
to ensure that the ~
resolves to your wwwroot folder:
<img src="@Url.Content(Model.Graphic)" />
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.