Reputation: 921
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.
Upvotes: 1
Views: 12376
Reputation: 22485
user1274646,
your best bet here is to create a public class that contains both the exisiting thumbnail element plus an additional FirstName property. Here's how this might look:
public class CandidateItem{
public string FirstName {get; set;}
public string Filepath {get; set;}
}
then, in your loop, create a new CandidateItem and add it to the list (i.e. List). Here's the amended code:
List<CandidateItem> srclist = new List<CandidateItem>();
foreach(var item in candidateportfolio)
{
if(item.PortfolioID!=0 && item.ChandidateID!=0)
{
CandidateItem candItem = new CandidateItem();
candItem.Filepath = Server.MapPath("~/ePortfolio/PortFolioContent/" + HobbyDetailID + "/Assignments/Exhb_" + item.PortfolioID + "_" + item.ChandidateID + ".jpg");
candItem.FirstName = item.FirstName;
if (System.IO.File.Exists(filepath))
{
srclist.Add(candItem);
}
}
}
ViewBag.Thumbnail = srclist;
then use it in the view as:
@foreach (var item in ViewBag.Thumbnail as List<CandidateItem>)
{
<img src="@item.Filepath" title="Learner:@item.FirstName" width="150px" height="150px" border="5" style="align:right;margin:10px"/>
}
Upvotes: 1
Reputation: 13256
If you want a list containing both FirstName
and the path to, say, a photo you can create a new class:
public class ThumbnailModel
{
public string FirstName { get; set; }
public string PhotoPath { get; set; }
}
Now you can add a List<Thumbnail>
to the ViewBag
.
But I would suggest just creating a strongly typed view with a relvant model containing a List<Thumbnail>
property.
Upvotes: 5