Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 861
quakkels Avatar asked Jun 30 '10 15:06

quakkels


People also ask

How do you pass an action model in HTML?

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.

What can you use to pass parameters to actions?

You can pass them via a URL, a query string, a request header, a request body, or even a form.

How do you pass an object from one action to another action in MVC?

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


1 Answers

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

Html.Action("ActionName","Controller", new { id = 1 }) 
like image 97
Roberto Hernandez Avatar answered Sep 19 '22 17:09

Roberto Hernandez