2

I'm trying to display every images in specific folders based on the folder name which equivalent to Model.Id.

@foreach (var imgPath in Directory.GetFiles(Server.MapPath("~/menu/" + Model.Id), "*.jpg"))
   {
         var img = new FileInfo(imgPath);
         <img src="@Url.Content(String.Format("~/menu/449/{0}", img.Name))" />
   }

In the last line, 449 is the folder name. How can I use Model.Id to replace 449? I'm not sure how to do that using String.Format

1 Answer 1

3
@foreach (var imgPath in Directory.GetFiles(Server.MapPath("~/menu/" + Model.Id), "*.jpg"))
   {
         var img = new FileInfo(imgPath);
         <img src="@Url.Content(String.Format("~/menu/{0}/{1}", Model.Id, img.Name))" />
   }

With string.Format you use numeric placeholders that match the position of the parameter included after the string. {0} is the 1st parameter, {1} the second and so on.

Sign up to request clarification or add additional context in comments.

2 Comments

Ha! It's simple as that! Thank you so much! :D
@jenna_3108 - Glad it worked. Please consider accepting an answer (see How to accept SO answers) once the 15 minutes period has expired.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.