Rendering content inside an Umbraco page

I have a doc type that allows users to pick alerts for pages using a multi node treepicker. Alerts are instances of another document type. Alerts don't have their own page so I wanted to use their selected template like a partial. What I want to do is something like:

var alertIds = Model.Content.GetPropertyValue("alert");

List<umbraco.NodeFactory.Node> alerts = new List<umbraco.NodeFactory.Node>();

foreach (var alertId in alertIds.ToString().Split(','))
{
    alerts.Add(new umbraco.NodeFactory.Node(int.Parse(alertId)));
}

Then as an example I could do:

library.RenderTemplate(alerts[0].Id)

I wanted to do it this way because I like the idea that the templates can be chosen in Umbraco and just know how to render themselves, rather than creating a partial in my MVC project and handling it on that side. However, I run into the following error:

"Error rendering template with id 1128: 'System.InvalidOperationException: A single instance of controller 'Umbraco.Web.Mvc.RenderMvcController' cannot be used to handle multiple requests. If a custom controller factory is in use, make sure that it creates a new instance of the controller for each request.\r\n at System.Web.Mvc.ControllerBase.VerifyExecuteCalledOnce()\r\n at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)\r\n at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)\r\n at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)\r\n at Umbraco.Web.Mvc.UmbracoMvcHandler.ExecuteUmbracoRequest()\r\n at Umbraco.Web.Templates.TemplateRenderer.RenderUmbracoRequestToString(RequestContext requestContext)\r\n at Umbraco.Web.Templates.TemplateRenderer.ExecuteTemplateRendering(TextWriter sw, PublishedContentRequest contentRequest)\r\n at Umbraco.Web.Templates.TemplateRenderer.Render(StringWriter writer)\r\n at umbraco.library.RenderTemplate(Int32 PageId, Int32 TemplateId)' "

Thanks in advance for checking this out!

Upvotes: 1

Views: 822

Answers (1)

Davor Zlotrg
Davor Zlotrg

Reputation: 6050

I see what you're trying but that's not the way to do it.

Alerts don't have their own page so I wanted to use their selected template like a partial.

If a document type (node) is not a page but just a container for data you should leave it without the template. It is because umbraco will generate a url for that node and you will mess up your SEO.

I wanted to do it this way because I like the idea that the templates can be chosen in Umbraco

You can create a dropdown property on your document type (new data type that will list all possible templates) to mimic the template selector. And when showing the node you would write something like:

switch(alert.GetPropertyValue<string>("template"))
{
    case "News": RenderForNews(alert);
    case "Frontpage": RenderForFrontpage(alert);
    ...
}

Upvotes: 1

Related Questions