Code sample

RenderContentData with support for rendering tag

EPiServer 7 CMS' API features two HTML helper extensions for rendering partial content with MVC, both named RenderContentData. None of them offer a way to render content using a rendering tag.

The below extension adds such a method:

using System.Web.Mvc;
using EPiServer;
using EPiServer.Core;
using EPiServer.Framework.Web;
using EPiServer.ServiceLocation;
using EPiServer.Web;
using EPiServer.Web.Mvc;
using EPiServer.Web.Mvc.Html;

namespace MySite
{
    public static class HtmlHelpers
    {
        public static void RenderContentData(
            this HtmlHelper html, 
            IContentData content,
            string tag,
            isInContentArea = false)
        {
            var templateResolver = ServiceLocator.Current.GetInstance<TemplateResolver>();
            var templateModel = templateResolver.Resolve(
                html.ViewContext.HttpContext,
                content.GetOriginalType(),
                content,
                TemplateTypeCategories.MvcPartial,
                tag);

            var contentRenderer = ServiceLocator.Current.GetInstance<IContentRenderer>();
            html.RenderContentData(
                content,
                isInContentArea,
                templateModel,
                contentRenderer);
        }
    }
}
Joel Abrahamsson

Joel Abrahamsson

I'm a passionate web developer and systems architect living in Stockholm, Sweden. I work as CTO for a large media site and enjoy developing with all technologies, especially .NET, Node.js, and ElasticSearch. Read more

More about EPiServer CMS