vrijdag 26 april 2013

Sitecore and MVC rendersections

Currently I'm working on a project for a customer who wants a Sitecore website based on ASP.NET MVC 4. After working on the project now for a couple of weeks I walked into some problems regarding Sitecore, MVC and RenderSections in Razor, here's what happened:

I created a standard layout in Sitecore, deriving from a Razor file which contained all HTML you normally would expect in a Razor layout file. After that I created a view rendering that derived from another Razor file, a partial file containing some extra information for the page. I actually didn't do anything to create a Sitecore content placeholder, I just referenced the view rendering by it's guid in the Sitecore layout Razor file. So far nothing special, as you would expect I had no problem showing the content from the view rendering on the layout.



Things got complicated when I tried to add a normal Razor RenderSection in the Sitecore layout Razor file, just adding this to the file triggered a yellow screen with the follow error message: "The file '/Views/Shared/_Layout.cshtml' cannot be requested directly because it calls the 'RenderSection' method". Allright, what just happened?! After some searching I found blog-posts saying I shouldn't put a Razor RenderSection on a Razor view file... Well ok, I didn't do that while I put my Razor RenderSection on a layout file, right?

No, not so right. While I've got a Sitecore layout item with a Razor view file referenced to it I actually didn't create a layout file. When you create a layout page in Sitecore and reference it to a Razor file you actually create a view page which isn't able to contain a Razor RenderSection and that's why I saw that yellow screen.

If you want to be able to include Razor RenderSections on your page you should create an actual Razor Layout file. You can do this by referencing another Razor file on top of the one you referenced in your Razor template that is included in your Sitecore layout item. In this new and 'real' Razor Layout file you can add Razor RenderSections, although there is a minor glitch when you setup your solution this way. 



Normally you would use the Razor RenderSections to place some content from your content item in a specific place in your Razor Layout page, you could for example create a Razor RenderSection for including page specific javascript at the bottom of your Razor Layout page. When you create a new page in Sitecore you basically create a content item based on a Sitecore Layout page. Your Sitecore Layout page normally would consist out of one or more renderings, which are a kind of Razor partial views.

Those renderings pose the real problem while Razor RenderSections can't be addressed from a Razor Partial view because it has no hard reference to it's Razor layout file. So if you'd like to have a RenderSection on a Sitecore rendering the default way; you're gonna have a bad time. The only way to add content to a Razor RenderSection is by declaring the content on the Sitecore content item, but that would also mean an extra dependency between the Sitecore content item and the Sitecore rendering while you'd have to manage content for one Sitecore rendering in two places.

There's no easy way to get around this problem. The best way probably is by creating two custom HTML helpers, one for accepting content to be displayed and one for rendering that content on a page. An example of the solution could be found on stackoverflow: http://stackoverflow.com/questions/5433531/using-sections-in-editor-display-templates/5433722#5433722.

Geen opmerkingen:

Een reactie posten