簡體   English   中英

在 asp.net core mvc 中通過@Html.ActionLink 傳遞參數

[英]passing parameter through @Html.ActionLink in asp.net core mvc

首先,我想澄清一下,我完全知道有很多類似的問題,但沒有一個對我有用。

我正在嘗試在我的 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返回值。

我在這里做錯了什么?

注意忽略控制器內部的代碼,它不完整,因為需要 xid 才能完成它。

你能試試這樣的嗎?

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

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

還要檢查這篇文章的答案

將參數從@Html.ActionLink MVC 4 傳遞給控制器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM