1

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.

Mill3r
  • 544
  • 1
  • 10
  • 31
  • What is the route for `GetImages` action? If it's default `{controller}/{action}/{id?}`', routing expects parameter named `id`, not `xid`. Because of that it ignores `xid` parameter. – Aleksei Petrov Oct 06 '20 at 07:28
  • It using the default route. and passing the parameter as **id** instead of **xid** does not work either. It still receives null. – Mill3r Oct 06 '20 at 07:42
  • Did you press F12 in your browser to check the generated url?What is it like?It could work well in my project.And your action name is GetImages,but you use Details in your anchor.I suggest that you could try to create a new project and test it if it works or not.If still not work,could you please share the new repo to us? – Rena Oct 07 '20 at 06:12

1 Answers1

1

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

G.Mich
  • 1,607
  • 2
  • 24
  • 42
  • I had already check the link you have provided. To my my code looks same to the answers given there but somehow it still does not work. However, how would changing it to async help my cause could you please elaborate if you dont mind. – Mill3r Oct 06 '20 at 07:47
  • From your sample code you call the "Details" Action from ActionLink but you provide the GetImages method. Is this just a sample or a real code ? Also check the controller name, "MarketPlaceController" looks normal. – G.Mich Oct 06 '20 at 07:58
  • Oh yes. I see that now. I'm changing that and trying it again. – Mill3r Oct 06 '20 at 08:01
  • I solved the problem but instead of using action link i used ajax request to fetch my data from database. – Mill3r Oct 06 '20 at 09:02
  • Congrats @Mill3r – G.Mich Oct 06 '20 at 09:03