返回给定路径中所有子目录的类型
本文关键字:子目录 类型 路径 返回 | 更新日期: 2023-09-27 18:33:46
嗨,我正在使用 C# 做我的项目 i MVC4。我正在尝试获取目录中的所有子目录,并将其列在我的视图中
为此,我正在编写以下代码
控制器
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;
}
我的代码是否正确? 那么控制器和模型中的返回类型是什么? 请帮助我
将控制器中的 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);
}
我没有尝试过,但这应该有效。 希望有帮助
将
foreach循环更改为以下
foreach (DirectoryInfo dir in subdir)
{
files.Add(dir.FullName);
}
在控制器中尝试此操作
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);
}