English 中文(简体)
如何在 MVC3 中使用 ViewBag 列表
原标题:How to use ViewBag as list in MVC3

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;

选项ableportfolio 是类候选组合的对象 。

然后在 View 中我像这样使用 :

 @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"/> 
}

现在的问题是,我还想要获取 ViewBag. firstName 作为列表。 我无法在此运行其它列表。 请告诉我该怎么做 。

问题回答

如果您想要包含 FirstName 和照片路径的列表, 您可以创建新类 :

public class ThumbnailModel
{
    public string FirstName { get; set; }
    public string PhotoPath { get; set; }
}

现在您可以将 List< Thumbnail> 添加到 ViewBag

但是,我建议仅仅用包含 < code> List< Thumbnail> 属性的 relvant 模型创建强烈的打印视图。

用户1274646,

您最好的赌注是创建一个公共阶级, 该阶级既包含解析缩略图元素, 也包含附加的 FirstName 属性。 这里这样看来 :

public class CandidateItem{
  public string FirstName {get; set;}
  public string Filepath {get; set;} 
}

然后,在您的循环中,创建一个新的候选项目,并将其添加到列表中(如列表)。

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;

然后在视图中使用它为:

@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"/> 
}




相关问题
Using jquery to get a partial view and updating the UI

I need to render a partial view (returned from the controller) to show some customer summary details. This will need to happen when the user clicks on a button. In the the mean time the user can ...

MVC 2 / MVC 3 / MVC 4

MVC 2 我们可以轻松地创造领域。 现在,我的问题涉及nes地区(地区内)。

Asp.Net MVC 2 - Changing the PropertyValueRequired string

Using a resx file in the App_GlobalResources directory, I ve been able to change the default message for the PropertyValueInvalid string of the model validators. But it doesn t work to translate the ...

ASP.NET MVC 3 - What features do you want to see? [closed]

I know a bunch of people that are really enjoying the improvements that ASP.NET MVC 2 made over the first release. I have just started to migrate our MVC 1 project over and so far areas has totally ...

热门标签