使用剃须刀显示特定文件夹中的所有图像
问题描述:
如何根据从模型传递的标识显示包含唯一文件夹名称的特定文件夹中的图像列表?使用剃须刀显示特定文件夹中的所有图像
例如,我有两个不同的文件夹,分别是“RestaurantA”和“RestaurantB”。
如果点击RestaurantA,我想显示文件夹“RestaurantA”中的所有图像,而不管图像名称如何。
<img src="~/menu/@(Model.Id)/1.jpg" class="materialboxed menu-photo" />
编辑
我忘了提及,每个文件夹中,图像的名称将是连续的形式。就像在“RestaurantA”文件夹中有1.jpg,2.jpg等我看到有人发布将不得不在Javascript中使用for循环。除了使用Javascript之外,还有其他方法吗?也许使用Razor语法。
答
您可以在CSHTML文件做这个里面剃刀代码:
@
{
//First get the directory on which your all your images reside
string strDirectory = Server.MapPath(Url.Content("~/*YourPathHere*/"));
//Get all files on the directory and store it on string array
string[] strFiles = Directory.GetFiles(strDirectory);
string strFileName = string.Empty;
//Loop on each file and attach it on img tag
foreach (var strFile in strFiles)
{
strFileName = Path.GetFileName(strFile);
<img id="myImg" src="@Url.Content("~/*YourPathHere*/" + strFileName)"/>
}
}
+0
是否有可能在图像标签中使用模型数据?像这样''bcos我试过了,出错了 –
从该文件夹内,并从该视图图像控制器发送列表,迭代产生多个img标签 –
'var中的文件=新的DirectoryInfo( Path.Combine(%文件夹路径%))GetFiles();' –
http://stackoverflow.com/questions/163162/can-you-call-directory-getfiles-with-multiple-filters – Searching