i need to use ViewBag as list inside another ViewBag and i am not understanding how should i do that.
here is my code:
List<string> srclist = new List<string>();
foreach(var item in candidateportfolio)
{
if(item.PortfolioID!=0 && item.ChandidateID!=0)
{
string filepath = Server.MapPath("~/ePortfolio/PortFolioContent/" + HobbyDetailID + "/Assignments/Exhb_" + item.PortfolioID + "_" + item.ChandidateID + ".jpg");
if (System.IO.File.Exists(filepath))
{
srclist.Add(filepath);
}
}
}
ViewBag.Thumbnail = srclist;
candidateportfolio is an object of the class CandidatePortfolio.
I fetch the data in the class and check whether its fields are not empty.Then i add the filepath to the list and assign list to Viewbag
Then in View i use it like this:
@foreach (var item in ViewBag.Thumbnail as List<string>)
{
<img src="@item" title="Learner:@ViewBag.FirstName" width="150px" height="150px" border="5" style="align:right;margin:10px"/>
}
Now the problem is i also want to fetch ViewBag.FirstName as list.and i cannot run another list in this.Please tell me how should i do this.
Best Answer
If you want a list containing both
FirstName
and the path to, say, a photo you can create a new class:Now you can add a
List<Thumbnail>
to theViewBag
.But I would suggest just creating a strongly typed view with a relvant model containing a
List<Thumbnail>
property.