How can I pass parameters to an Action using Html.Action() in ASP.NET MVC?

asp.netasp.net Mvcasp.net Mvc-2

asp.net Problem Overview


I've been using Html.Action("ActionName", "ControllerName") to invoke child actions across controllers without needing to have the view in Views\Shared. This has been working great for displaying things like session or cookie information.

Instead of just accessing cookies, I would like to pass additional parameters to Html.Action("ActionName", "ControllerName") so the action can execute different code based on the the data passed to the original view.

Should I be using a different method to pass parameters to a child action in a different controller? How would one accomplish this?

asp.net Solutions


Solution 1 - asp.net

You could specify additional data in the RouteValues property like this.

Html.Action("ActionName","Controller", new { id = 1 })

Solution 2 - asp.net

To add a little on this question, I am using ASP.Net MVC 5 and I could succeed to achieve this with this code:

@Html.Action("foo",new {parameter1=1})

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionquakkelsView Question on Stackoverflow
Solution 1 - asp.netRoberto HernandezView Answer on Stackoverflow
Solution 2 - asp.netcodeaView Answer on Stackoverflow