Q
Q
Quelty2019-02-02 19:18:46
HTML
Quelty, 2019-02-02 19:18:46

How to call a controller method with a parameter via ActionLink?

I want to display a post via a separate Post function:

public ActionResult Post(Guid postId)
        {
            MagentaEntities context = new MagentaEntities();

            var post = context.Post.FirstOrDefault(p => p.PostId == postId);
            ViewBag.Post = post;

            return View("~/Views/Forum/Post.cshtml");
        }

however, when generating a list of ActionLink posts and then following the link, the Guid is empty.
@foreach (KeyValuePair<Guid, string> item in postsToHeaders)
   {
        <div>@Html.ActionLink(item.Value, "Post", new { id = item.Key })</div>
   }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Susarov, 2019-02-12
@slcoleg

The parameter name must match the parameter name in the controller ( postId instead of id )

<div>@Html.ActionLink(item.Value, "Post", new { postId = item.Key })</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question