【问题标题】:@Html.ActionLink from PartialView not finding my controller actionPartialView 中的@Html.ActionLink 找不到我的控制器操作
【发布时间】:2021-08-06 09:27:09
【问题描述】:

我正在尝试创建一个操作链接来下载文件。我在视图中获得了 noticiaId 和文件名的值,但在控制器中没有。 我缺少的 actionlink HTML 帮助程序有问题。 如果我这样做:

 @Html.ActionLink("Download", "DownloadAnexo", "NoticiasController", new { noticiaid = Model.ID, filename = f.File }, null)

我通过以下 URL 获得 HTTP 404:localhost:port/NoticiasController/DownloadAnexo?noticiaid=55&filename=file.png 这对我来说看起来是正确的,但我仍然收到 404 错误,说找不到路线。

局部视图:

@model Web.Models.Noticias.NoticiaVM
<div class="col">
        
        <label>Anexos:</label>
        <table>
            @if (Model.Anexos != null)
            {
                foreach (var f in Model.Anexos)
                {
                    <tr>
                        <td>@f.File</td>
                        <td>
                            @Html.ActionLink("Download", "DownloadAnexo", "NoticiasController", new { noticiaid = Model.ID, filename = f.File }, null)
                        </td>
                    </tr>
                }
            }
        </table>
    </div>

控制器动作:

public ActionResult DownloadAnexo(int? noticiaid, string filename)
    {
        if(noticiaid != null && !String.IsNullOrEmpty(filename))
        {
            string fullPath = ConfigurationManager.AppSettings["NoticiasFiles"] + "/N_" + noticiaid + "/Anexos/";
            byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath);
            return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, filename);
        }
        else
            return Content("");
    }

我也试过这个:

<a href="@Url.Action("DownloadAnexo", "NoticiasController", 
     new { noticiaId = Model.ID, filename = f.File })">
 Download
</a>

但我得到了相同的结果。 我对局部视图没有太多经验,所以我想知道是否只是因为我将数据从局部直接发送到控制器,而这可能是不可能的。 IDK。

请帮忙。

【问题讨论】:

  • 混淆了术语,使您的问题更加混乱。 localhost:port/NoticiasController/DownloadAnexo?noticiaid=55&amp;filename=file.png 是 URL,而不是路由,@Html.ActionLink 是 HTML 助手,而不是 tag helper
  • 你在DownloadAnexo上有[HttpPost]吗?
  • 对术语感到抱歉。我需要在这方面做得更好。谢谢。
  • 是的,我有 HttpPost
  • 这能回答你的问题吗? ActionLink not calling Controller

标签: c# asp.net .net asp.net-mvc


【解决方案1】:

你打错了,NoticiasController 应该是 Noticias

@Html.ActionLink("Download", "DownloadAnexo", "Noticias", new { noticiaid = Model.ID, filename = f.File }, null)

并修复动作

public ActionResult DownloadAnexo([FromQuery] int? noticiaid, [FromQuery]string filename)

【讨论】:

  • 是的。刚看到。很抱歉浪费了你的时间。谢谢。
  • @GSerg 我什么都试过了。这只是一个该死的错字。无论有没有它,它都没有击中动作,所以这不是我遇到的问题。顺便说一下,谢谢你的提示。
猜你喜欢
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多