Search code examples
asp.net-mvcnullactionlink

Passing ViewBag value from ActionLink to Controller


I am trying to pass the privApplicationId (retrieved from the ViewBag) to an ActionResult in my controller

Layout Cshtml page:

 @Html.ActionLink("Comments", "ViewComments", "SubmittedApplications", new { @area = "Privileging" }, new { privApplicationId = @ViewBag.PrivApplicationId })

However, everytime it goes into the ActionResult, the privApplicationId value is null.

Controller ActionResult:

 public ActionResult ViewComments(long? privApplicationId)

Can you please help me?


Solution

  • You picked the wrong overload of ActionLink helper. new { privApplicationId = @ViewBag.PrivApplicationId } is passed as htmlAttribute parameter, but should be routeValue. Try this way:

    @Html.ActionLink("Comments", "ViewComments", "SubmittedApplications",
          new { area = "Privileging", privApplicationId = @ViewBag.PrivApplicationId }, null)