ASP.NET MVC: Render a Partial View in a MasterPage or another view

Usualy a MVC Page is rendered using a Controller which loads a Model and supplies the data to the configured View which displays the data.

11 January 2011
Christoph Keller Christoph Keller

Usualy a MVC 2 Page is rendered using a Controller which loads a Model and supplies the data to the configured View which displays the data.

Sometimes it is required to render more than one View (or render multiple Partial Views) at one output Page. This mostly makes sense in MasterPages (for example for a Navigation menu) or if you have a detail view of a item and want to display the whole list on the same page.

To accomplish this task, you have to use the 'Html.Action' or 'Html.RenderAction' Method, available at most of the view-types (ViewPage<>, ViewUserControl<>, ViewMasterPage<>)

You can use either 'Html.Action':

<%= Html.Action("Index", "Navigation") %>

or 'Html.RenderAction':

<% Html.RenderAction("Index", "Navigation"); %>

The difference between the two possibilities is that 'Html.Action' returns a string with the rendering-result, 'Html.RenderAction' directly writes the rendering-result to the response output.


comments powered by Disqus