微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在ASP.NET Core MVC中通过@ Html.ActionLink传递参数

如何解决在ASP.NET Core MVC中通过@ Html.ActionLink传递参数

首先,我想澄清一下,我完全意识到有很多类似的问题,但是没有一个我有用。

我正在尝试在我的ASP.NET Core 3.1 MVC应用程序中使用@ Html.ActionLink。

我从控制器接收到一个id并将其保存在我的视图中的变量中

@{
   string attachmentToFecth = Model.AttachmentId;
 }

稍后,我试图将其通过ActionLink传递给控制器​​,以获取诸如此类的一些数据

@Html.ActionLink("","Details","MarketPlace",new { xid = attachmentToFecth },null)

我的控制器功能如下

    [HttpGet]
    public ActionResult Getimages(string xid)
    {
        List<string> Images = new List<string>();
        var userID = _applicationUser.Users.FirstOrDefault(obj => obj.UserName == User.Identity.Name).UserId;

        var displayImages = (from images in _context.Attachments
                             join imageID in _context.InfoProducts on images.AttachmentId equals imageID.AttachmentId
                             
                             select new
                             {
                                 images.AttachmentPath
                             });

        return View(displayImages);
    }

当我在ActionLink上放置一个断点时,可以看到正在接收预期的ID

新{xid = attachmentToFecth}

但是控制器中的字符串xid 返回值。

我在做什么错了?

N.B忽略控制器中的代码,因为xid是完成代码所需要的,所以代码不完整。

解决方法

您可以尝试这样的事情吗?

@Html.ActionLink("My Link","GetImages","MarketPlace" new { id = attachmentToFecth  },null)

public async Task<Actionresult> GetImages(string id)
{
    // Do stuff with the id
}

也请查看这篇文章的答案

Pass parameter to controller from @Html.ActionLink MVC 4

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。