Answer the question
In order to leave comments, you need to log in
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");
}
@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
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 questionAsk a Question
731 491 924 answers to any question