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

by Christoph Keller 11. January 2011 13:20

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.

 

DotnetKicks
DotNetShoutout

Tags: , ,

ASP.NET | MVC 2 | CodeProject

blog comments powered by Disqus