如何在循环中显示来自Web应用程序项目的外部图像?

时间:2014-03-29 05:54:28

标签: c# asp.net-mvc asp.net-mvc-4 razor

正如标题所说,我试图通过这种方式将外部图像循环显示在ASP.NET MVC 4网络应用程序中的一个视图中:

for(int i= 1; true; i++){
   string filepath = "C:\imgs\" + i + ".png";
   do{
       bool exists = filepath.exists;
   } while(!exists);
   displayImage(filepath);
}

请问如何处理?

1 个答案:

答案 0 :(得分:0)

// model
class RenderImageModel
{
   List<string> UserImages {get;set;}

   public RenderImageModel(){
        UserImages = new List<string>();
   }
}

// controller action
public ActionResult Index(){
    var model = new RenderImageModel();
    for(int i= 1; true; i++){
       string filepath = "C:\imgs\" + i + ".png";
       do{
           bool exists = filepath.exists;
       } while(!exists);
       model.UserImages.Add(filepath);
    }

    return View(model);
}

// view
@foreach(imagePath in Model.UserImages){
    <img src='@imagePath' alt />
}