1.controller:
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
List<MyFileInfo> infoList = new List<MyFileInfo>();
DirectoryInfo theFolder = new DirectoryInfo(Server.MapPath("~/Files/"));
FileInfo[] fileInfo = theFolder.GetFiles();
fileInfo.ToList().ForEach(e => {
string filename=e.Name;
infoList.Add(new MyFileInfo() {FileName=filename.Substring(0,filename.LastIndexOf(".")), FilePath="/Files/"+filename });
});
ViewBag.dataList = new SelectList(infoList, "FilePath", "FileName");
return View();
}
}
public class MyFileInfo
{
public string FileName { get; set; }
public string FilePath { get; set; }
}
2.cshtml:
<div>
@Html.DropDownList("FileList", ViewBag.dataList as SelectList, "--请选择--", new { width="auto" })
</div>