views:

94

answers:

1

I'm wondering if it is possible to render a ViewPage inside of a ViewPage.

Normally, you'd have this:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<my_object>"%>

and then in the page you can render a user control:

<%Html.RenderPartial("~/Views/Shared/mycontrol.ascx", this.ViewData);%>

However I'd like to render a ViewPage in a ViewPage. The reason? I'm working on a very large project with 10+ developers and a lot of code already written. A large piece is already written which dynamically renders the UI however there are 15+ methods which pass around a ViewPage.

In other words, I'm calling this in the ViewPage:

<%this.RenderUI();

And the method stub is this:

public static void RenderUI(this ViewPage viewPage)

So in order to reuse the most code, I'd like to pull the call to this.RenderUI() into it's own ViewPage but then include that on some ViewPages. It's a hairy situation and it sounds unnecessary, but there's a lot of code rework that would need to be done otherwise. Does anyone know if this can be achieved?

+1  A: 

Render your subview in the main view using RenderAction() instead of RenderPartial(). This allows you to keep your controller and view for each subview, and "inject" the output of each controller/subview combination into any main view, in a location of your choosing within the main view.

This arrangement should ease some of the complexity of managing each of the views and subviews. It is an approach reminiscent of the "web parts" in traditional ASP.NET or SharePoint. It is a more modular approach that provides for better reusability.

Robert Harvey
Thanks I like this idea. I'm going to give it a try. Does anyone else have suggestions?
macca1