【问题标题】:How to pass model plus additional parameter from view to controller如何将模型和附加参数从视图传递到控制器
【发布时间】: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


【解决方案1】:

您不应该像这样存储data = Model 会导致性能和安全问题。

您只需从 View 中存储一个 fileToDownload 值。之后,在Controller中,你应该通过fileToDownload参数获取文件。

[HttpPost]
public ActionResult DownloadFile(string fileToDownload)
{
    // download the file by `fileToDownload` param here

    return RedirectToAction("Index");
}

【讨论】:

  • @FLICKER 这能回答你的问题吗?如果您需要任何帮助,请告诉我
  • 我怎样才能让模型回到控制器中?
【解决方案2】:

您收到该错误的原因是,它试图进行表单发布,并且没有代表 fileToDownload 和数据的表单 html 元素。

实际上,您可以使用锚标签调用控制器的 GET 操作,而不是使用表单 POST(因为它只是下载,这里不需要使用 POST)。正如@Phong 提到的,使用 fileToDownload 您可能可以检索重定向到索引所需的其他信息。

@foreach (var file in Model.Files)
{
    <a href='@Url.Action("DownloadFile", "SharedFolder", new { fileToDownload = file})' title="Click here to download the file">@file</a>
}

然后您的控制器操作将是:

public ActionResult DownloadFile(string fileToDownload)
{
    // do the stuff
    // download would return FileResult on success, not sure what you meant by RedirectToAction("Index")
    // download the file and return Index view
    return RedirectToAction("Index");
}

如果您仍然想发送模型,那么您可以通过 ajax 的 javascript 按钮单击事件来实现。

【讨论】:

  • 感谢您的回答,让我了解如何使用 ajax 将模型发送回控制器。我也想下载一个文件,我需要POST。不知道我是否可以使用 ajax 下载
  • 如果您想下载文件,为什么要使用 RedirectToAction?我认为,您仍然无法使用 Form Post 做两件事 - 下载和重定向。试图了解您的要求 - 为什么需要模型 - 我的意思是目的?
  • 我在下载文件后重定向。我想我可以做到,因为在 ASP WebForms 中,我已经完成了下载和重定向。所以我想我也可以在这里做到。
  • 据我所知,我认为你不能在 MVC 中同时做到这两点。下载文件后,需要返回文件内容正确吗?返回文件内容后,您将超出此操作范围。你怎么还能重定向?顺便问一下,为什么需要模型?
  • 我会试一试,然后告诉你。
猜你喜欢
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
  • 2018-07-10
  • 1970-01-01
相关资源
最近更新 更多