2013-10-04 106 views
1

您好我正在做我的項目我使用c#mvc4。我試圖讓在directory.and列表所有子目錄在我看來從給定路徑返回所有子目錄的類型

爲我寫了下面的代碼

控制器

public ActionResult Gallery() 
    { 
     string folderpath = Server.MapPath("~/Content/Gallery/GalleryImages"); 
     List<string> currentimage = new Gallery().GetGalleryName(folderpath); 
     //What will be the return type???/ 
     return View(currentimage); 
    } 

模型

public List<string> GetGalleryName(string path) 
    { 
     DirectoryInfo di = new DirectoryInfo(path); 
     DirectoryInfo[] subdir = di.GetDirectories(); 
     List<String> files = new List<String>(); 
     foreach (DirectoryInfo dir in subdir) 
     { 
      var name = dir.Name; 
      files.Add(name); 
     } 

     return files; 
    } 

我的代碼是正確的嗎?那麼控制器和模型中的返回類型是什麼?請幫我在控制器

+0

你得到任何錯誤或東西 ? –

+0

嘗試運行代碼並檢查它是否正確 –

+0

@ SaghirA.Khatri:ya error in return subdir;和string [] currentimage = new Gallery()。GetGalleryName(folderpath); – neel

回答

1

更改foreach循環

foreach (DirectoryInfo dir in subdir) 
     { 
      files.Add(dir.Name); 
     } 

public ActionResult Gallery() 
    { 
     string folderpath = Server.MapPath("~/Content/Gallery/GalleryImages"); 
     string[] currentimage = new Gallery().GetGalleryName(folderpath); 
     //What will be the return type???/ 
     return View(currentimage); 
    } 

public ActionResult Gallery() 
    { 
     string folderpath = Server.MapPath("~/Content/Gallery/GalleryImages"); 
     List<String> currentimage = new Gallery().GetGalleryName(folderpath); 
     //What will be the return type???/ 
     return View(currentimage); 
    } 

我hvn't嘗試過改變你的控制器,但這應該工作。希望它有助於下面

foreach (DirectoryInfo dir in subdir) 
    { 
        files.Add(dir.FullName); 
    } 
+0

文章:謝謝..它工作 – neel

+1

如果它已經比plz工作接受答案:) –

1

變化foreach循環在你的控制器試試這個

public ActionResult Gallery() 
{ 
    List<String> galleryList = new List<String>(); 
    string folderpath = Server.MapPath("~/Content/Gallery/GalleryImages"); 
    string[] currentimage = new Gallery().GetGalleryName(folderpath); 
    foreach (var folder in currentimage) { 
    galleryList.Add(folder); 
    } 
return View(galleryList); 
} 
相關問題