0
votes

I have Razor working minimally with master-pages using the following technique:

http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx

This works almost perfectly (Calling RenderPartial from the MasterPage on the RazorView.) The only short coming is I have 3 content blocks (controls) in my master page. Conceptually this maps nicely to a Razor Layout with 3 sections by the same names.

The only problem is I can;t figure out how to pull each section of a razor layout individually to Render it into the masterpage sections.

I tried setting the ViewBag.Layout in each masterpage section and the having 3 Razor layouts rendering the 3 razor-layout based sections [footer, header, and body]. The problem is Razor doesn't let me Render a razor view implementing multiple sections through a layout only rendering just one section. (It forces me to render all 3.)

My other attempt was to have the razor layout render the master page control markup along with the razor views. This seems elegant, but the I get an error that the masterpage control collection is read only.

Is there any way that allows me to develop Razor views to a Razor layout and have multiple layout sections map to a master pages equivalent multiple (one works perfectly) content blocks/sections?

Edit: Updated Link to the MasterPage/MVC integration article.

1
I really don't understand what you're trying to do. Can you clarify? What exactly do you mean by "pull each section of a razor layout individually to Render it into the masterpage sections."? - Erik Funkenbusch
Layouts have sections. Master pages also have sections (I believe formally referred to as Content Controls). I simply which to extract sections from a razor layout and inject them into the equivalent master page content blocks using Html.RenderPartial. I can't convert everything to Razor and so I need to make use of existing master pages, but would still like to use Razor. - Joshua Enfield

1 Answers

1
votes

Layouts and Master Pagers are two different things. You can't mix and match them in the same pages. You're going to have to recreate the sections of master pages in layouts if you want to convert. Over time, you can get rid of the duplication by getting rid of the master pages.

What you could do, is extract the relevant part of both master pages into a single partial page, then inject that into both Layout and Master pages using a partial. So your content section and razor section would both just contain a single Html.Partial() that injects the common code into both.