我正在尝试从特定目录中检索.dll文件的文件信息列表,以显示在ASP.NET网页上。我想要显示的信息是文件名,上次修改日期和版本。
到目前为止,我将数据存储在ViewBag中并显示在视图中,但它很混乱,我希望它显示在表格中。
有没有办法从ViewBag中获取数据并将其放在表格中,还是有比使用ViewBag更好的方法?
到目前为止,这是我对View的代码:
@using System.Diagnostics
@{
ViewBag.Title = "Versions";
}
<h2>Versions</h2></br>
<h3>File Name Last Modified Version</h3>
@ViewBag.FileList
@for(int i =0; i < ViewBag.FileList.Length;i++)
{
<p>
@ViewBag.FileList[i];
@{ FileInfo f = new FileInfo(ViewBag.FileList[i]);
<table>
<td><tr>@f.Name</tr></td>
<td><tr> @f.LastAccessTime</tr></td>
</table>
FileVersionInfo currentVersion = FileVersionInfo.GetVersionInfo(ViewBag.FileList[i]);
@currentVersion.FileVersion
}
</p>
}
答案 0 :(得分:1)
不应该以这种方式利用ViewBag。使用视图模型
在你的控制器的动作中传递这样的数据,
public ActionResult Files()
{
List<FileInfo> fileNames = ...get file names
return View(fileNames);
}
在你看来, 在顶部,定义对象的类型
@model IEnumerable<System.IO.FileInfo>
你的桌子应该用类似下面的方式布置。
<table>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Name</td>
<td>@item.LastAccessTime</td>
</tr>
}
</tbody>
</table>