58

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?

0

2 Answers 2

118

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

Html.Action("ActionName","Controller", new { id = 1 })
Sign up to request clarification or add additional context in comments.

4 Comments

Aw Sweet!!! I saw RouteValues in the intelisense but I didn't use that format. I tried Html.Action("Action", "Controller", model.Paramter). I'm gonna try this right away.
How do you pass a string, like the text, or the id of an auto populated drop-down list item?
@antman1p That sounds like a new question, not a comment. Post a link to the new question here and I will help as much as possible. One thing, it sounds like what you are trying to do happens at the client, as it depends on interaction with the user (browser), while this question in particular refers to how to get a link to an action that includes a parameter that is rendered on a view on the server side.
1

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})

1 Comment

That is an overload of the same method. In that overload it is assumed you are calling the same controller that was used to render the current view and the action is "foo". There are couple of other overloads worth mentioning: msdn.microsoft.com/en-us/library/…

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.