【发布时间】:2020-02-25 00:02:53
【问题描述】:
在我看来,使用 ASP.NET MVC,我列出了一些文件并将它们显示为一个按钮。通过单击每个按钮,应下载相应的文件。
为了显示文件列表,我将模型传递给查看,当用户单击每个按钮时,我必须将文件名和原始模型发送回控制器。
我找到了类似问题的答案,但就我而言,我没有一个按钮。我在视图中呈现的每个文件名都有一个按钮。
这是我的观点的代码:
@using (Html.BeginForm("DownloadFile", "SharedFolder", FormMethod.Post))
{
<div class="col-sm-6">
<div class="panel panel-info">
<div class="panel-heading">Files</div>
<div class="panel-body" style="max-height:300px; height:300px; overflow-y:scroll">
@foreach (var file in Model.Files)
{
<button type="button" class="btn btn-link btn-sm" onclick="location.href='@Url.Action("DownloadFile", "SharedFolder", new { fileToDownload = file, data = Model })'">
<div class="glyphicon glyphicon-file" style="color:dodgerblue">
<span style="color:black;">@file</span>
</div>
</button>
<br />
}
</div>
</div>
</div>
}
还有我的控制器操作:
[HttpPost]
public ActionResult DownloadFile(string fileToDownload, FolderExplorerViewModel data)
{
// download the file and return Index view
return RedirectToAction("Index");
}
当我点击文件进行下载时,出现以下错误:
找不到资源。
请求的 URL:/SharedFolder/DownloadFile
更新:
这是我的视图模型
public class FolderExplorerViewModel
{
public int ID { get; set; }
public List<Folder> Folders { get; set; }
public List<string> Files { get; set; }
public string SelectedPath { get; set; }
}
【问题讨论】:
-
请向我们展示您的
FolderExplorerViewModel结构。为什么需要这个? -
我添加了它,但不确定它是否有助于获得答案。
标签: asp.net asp.net-mvc asp.net-mvc-5