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?
Html. Action has an overload that expects route values as an object. you could trying passing the model there and model binding will kick in. Your action has to be expecting a parameter of type Model though.
You can pass them via a URL, a query string, a request header, a request body, or even a form.
You can use TempData to pass the object. This worked well, Thanks Eranga. One potential pitfall is that the object can be disposed before it gets used in the view if you have a overridden definition for Dispose in your controller(something VS tends to add in with its auto generated CRUD code).
You could specify additional data in the RouteValues property like this.
Html.Action("ActionName","Controller", new { id = 1 })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With