让我的视图正确生成有一点问题。
我收到错误:模型不包含“GetEnumerator”的公共定义
我试图将我的通用列表更改为 IEnumerable 列表,但是它在我无法摆脱的代码中弹出了更多错误,我不确定是否必须以某种方式将它添加到我的 UploadedFile 类中?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Mvc_fileUploader.Models
{
public class UploadedFile
{
public string Name { get; set; }
public string Path { get; set; }
public long Size { get; set; }
}
}
控制器:
[HttpGet]
public ActionResult UploadedFiles()
{
var uploadedFiles = new List<UploadedFile>();
var files = Directory.GetFiles(Server.MapPath("~/fileUploads/"));
foreach (var file in files)
{
var fileInfo = new FileInfo(file);
var uploadedFile = new UploadedFile();
uploadedFile.Name = Path.GetFileName(file);
uploadedFile.Size = fileInfo.Length;
uploadedFile.Path = ("~/fileUploads/") + Path.GetFileName(file);
uploadedFiles.Add(uploadedFile);
}
return View();
看法:
@model Mvc_fileUploader.Models.UploadedFile
@{
ViewBag.Title = "UploadedFiles";
}
<h2>UploadedFiles</h2>
<table style="background-color:lightpink; border:solid 2px black;">
<tr>
<td>Name</td>
<td>Size</td>
<td>Preview</td>
</tr>
@foreach (var file in Model)
{
<tr>
<td>@file.Name</td>
</tr>
}
</table>
github上的源码: https ://github.com/xoxotw/mvc_fileUploader