Click here to Skip to main content
15,307,769 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
The code so far

I have made a List in the controller from a directories file names.

Controller
C#
List<string> Pictures = new List<string>();
            DirectoryInfo getPic = new DirectoryInfo(@"mycomputter");
            FileInfo[] fType = getPic.GetFiles("*.png");
            
            foreach(FileInfo ft in fType)
            {
                Pictures.Add(ft.Name.ToString());  
            }
            ViewBag.MyList = Pictures;
            return View();


View
C#
@foreach (var item in ViewBag.MyList)
   {
       <tr>
           <td>
             <img src="~/Content/Images/@item" style="height:150px; width: 250px;" />
           </td>
       </tr>

   }


The Question
After this I loop its contents and create a grid of pics, which is what I initially wanted. In Desktop Application I would use for example some code like below

List<string> 70sPop = new List<string();>
70spop.add("abba");
70spop.add("beegees");
70spop.add("boneym");
to get boneym in a console application I would use
console.writeline(mcsc[2]); view if wanted solely to use the 3rd element. I have found Viewbag.mylist[x], just yields the code the itself to the view. I would like just use one element to produce a string. Look for information elsewhere almost all solutions pointed to a loop. I could use some conditional statements in the foreach loop but I am looking if poosible to call an individual element.
Posted
Updated 23-Dec-15 3:59am
v4
Comments
Nathan Minier 22-Dec-15 12:56pm
   
You know that you're returning the view before the filenames are added, right?
MarcusCole6833 22-Dec-15 12:58pm
   
clerical error in the cut and Paste I will correct! (I sound like Gloria Gaynor)
DotNetSteve 22-Dec-15 22:44pm
   
can you please explain this more clearly? - How do I use the individual constituents of a List in a View?
MarcusCole6833 23-Dec-15 9:49am
   
I have added a little to the above in relation to your question

1 solution

I'm not completely sure that I understand you're issue. I've put the same basic code into place and it seems to work fine.

I can provide a list of the document names and I can select an individual item to display within an tag. The code for my View is shown below:

<br />
<ul><br />
	@foreach (var item in ViewBag.FileList)<br />
	{<br />
		<li>@item</li><br />
	}<br />
</ul><br />
<br /><br />
<br /><br />
Item Number 2: <br /><br />
<img src="~/Content/Employees/@ViewBag.FileList[2]" /><br />
   
Comments
MarcusCole6833 23-Dec-15 14:10pm
   
Thank you for your help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900