我正在用blazor和我的项目中创建一个上传/下载网站。我有一个index.razor文件和一个index.razor.cs文件,我把我的索引模型放在那里,它引用了dbcontext等。(见下面的代码(
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly UploadFileContext _context;
public IndexModel(ILogger<IndexModel> logger, UploadFileContext context)
{
_logger = logger;
_context = context;
}
public IList<PdfFile> Files { get; set; }
public void OnGet()
{
Files = _context.Files.ToList();
}
public async Task<IActionResult> OnPostDownloadAsync(int? id)
{
var myInv = await _context.Files.FirstOrDefaultAsync(m => m.Id == id);
if (myInv == null)
{
return NotFound();
}
if (myInv.Attachment == null)
{
return Page();
}
else
{
byte[] byteArr = myInv.Attachment;
string mimeType = "application/pdf";
return new FileContentResult(byteArr, mimeType)
{
FileDownloadName = $"{myInv.FileType} {myInv.Number}.pdf"
};
}
}
public async Task<IActionResult> OnPostDeleteAsync(int? id)
{
var myInv = await _context.Files.FirstOrDefaultAsync(m => m.Id == id);
if (myInv == null)
{
return NotFound();
}
if (myInv.Attachment == null)
{
return Page();
}
else
{
myInv.Attachment = null;
_context.Update(myInv);
await _context.SaveChangesAsync();
}
Files = await _context.Files.ToListAsync();
return Page();
}
}
我试图在主剃刀页面中引用我的ilist,我将在foreach循环中使用它,以显示每个文件的名称和文件类型。
我该怎么做?
您可以在Razor页面中使用Files
定义foreach循环。
@if (Files.Count > 0)
{
<ol>
@foreach (var file in Files)
{
<li>@file.Name</li>
<li>@file.FileType</li>
}
</ol>
}