I need to display employee names and their corresponding image below on a blazor webpage, So far I have the names displayed fine but can only loop through one image due to not knowing how to loop through the wwwroot folder so each name has the same image.
<h1>Northwind Employees</h1>
@foreach (Employee employee in NorthWindEmployees)
{
<h3 style="text-align: center;">
@employee.FirstName @employee.LastName <br>
</h3>
<div width="100" height="100" style="vertical-align:middle; text-align:center">
<img src="/images/EmpID1.jpg" /> //the reason for each name having the same image
</div>
}
@code {
public List<Employee> NorthWindEmployees;
protected override void OnInitialized()
{
Northwind db = new Northwind();
NorthWindEmployees = db.Employees.ToList();
}
}
How do I loop through the \wwwroot\images\ folder to display each employee image with their name?
Let's say, you have Image folder within your wwwroot. In this scenario, you would need to access that image as following:
As you can see, I have ImageNameFolder inside wwwroot so you would access it as following:
When you would have List:
Let's assume your NorthWindEmployees model looks as like below:
Therefore, you would access it as following:
Note: You can use styte either in your src or in div as well.
Complete Demo:
Output:
Note: If you would like to know more details on it you could check our official document here.