无法在Razor foreach循环中使用Viewbag列表项

ric*_*y89 3 c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我有一个链接到Item表的Images表(一个Item可以有很多Images).它们由Item Id链接.

我想在详细信息页面上返回项目的所有图像.我一直在尝试使用Viewbag来实现这个目标(下面).

控制器:

ViewBag.imageFile = (from f in storeDB.Images
                               where f.ItemId == id
                               select f.ImageURL).ToList();    
Run Code Online (Sandbox Code Playgroud)

视图:

@foreach (var image in ((List<string>)ViewBag.imageFile)) 
{
    @ViewBag.imageFile    
}
Run Code Online (Sandbox Code Playgroud)

以上将为每个ImageUrl返回'System.Collections.Generic.List`1 [System.String]'.我认为问题是对象是返回而不是实际的List项字符串值,但无法弄清楚如何将其转换为我需要的字符串.计划是编辑此foreach以为每个URL创建img链接.

我也有以下Viewbag(测试看我能够至少获得Urls):

ViewBag.imageURL = string.Join(",", ViewBag.imageFile);
Run Code Online (Sandbox Code Playgroud)

以上内容返回正确的URL,但它们都在一个以逗号分隔的字符串中.

感谢任何帮助.

And*_*bel 10

在视图的循环内部,您应该使用循环变量:

@foreach (var image in ((List<string>)ViewBag.imageFile)) 
{
    @image
}
Run Code Online (Sandbox Code Playgroud)

ViewBag.imageFile是完整的列表,即使在循环内也是如此.该image循环变量将是从循环动辄列表中的当前项目.