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.

3
  • 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. Commented Oct 6, 2020 at 7:28
  • It using the default route. and passing the parameter as id instead of xid does not work either. It still receives null. Commented Oct 6, 2020 at 7: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? Commented Oct 7, 2020 at 6:12

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
Oh yes. I see that now. I'm changing that and trying it again.
I solved the problem but instead of using action link i used ajax request to fetch my data from database.
Congrats @Mill3r

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.