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

2.5k Views Asked by At

First of all I would like to clarify that I am fully aware that there are many similar questions but none of them worked for me.

I am trying to user @Html.ActionLink in my ASP.NET Core 3.1 MVC application.

I am receiving an id from the controller and saving it in a variable in my view as such

@{
   string attachmentToFecth = Model.AttachmentId;
 }

Later I am trying to pass it through ActionLink to my controller to fetch some data like this

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

My controller function looks like this

    [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);
    }

When I put a breakpoint at the ActionLink I can see that the expected id is being received in

new { xid = attachmentToFecth }

But the string xid in the controller is returning null value.

What am I doing wrong here?

N.B Ignore the code inside the controller, its incomplete as the xid is required to complete it.

1

There are 1 best solutions below

5
On

Can you try something like this please ?

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

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

Also check the answers from this post

Pass parameter to controller from @Html.ActionLink MVC 4