0

How to display list of images from specific folder that contains unique folder name based on the Id passed from model?

For instance, I have two different folders namely "RestaurantA" and "RestaurantB" respectively.

If RestaurantA is clicked, I want to show all the images in folder "RestaurantA", regardless of the image name.

<img src="~/menu/@(Model.Id)/1.jpg" class="materialboxed menu-photo" />

Edit

I forgot to mention that in each folder, the names of the image will be in sequential form. Like in "RestaurantA" folder has 1.jpg, 2.jpg etc. I saw someone posted will have to use for loop in Javascript. Is there any other way other than using Javascript? Perhaps using Razor syntax.

jenna_3108
  • 437
  • 1
  • 7
  • 20

1 Answers1

2

You can do this inside razor code at your cshtml file:

@
{
//First get the directory on which your all your images reside
string strDirectory = Server.MapPath(Url.Content("~/*YourPathHere*/"));

//Get all files on the directory and store it on string array
string[] strFiles = Directory.GetFiles(strDirectory);

string strFileName = string.Empty;

//Loop on each file and attach it on img tag
    foreach (var strFile in strFiles)
      {
         strFileName = Path.GetFileName(strFile);
         <img id="myImg" src="@Url.Content("~/*YourPathHere*/" + strFileName)"/>
      }   
}
Willy David Jr
  • 8,604
  • 6
  • 46
  • 57