3

I need to load renderings and assign Datasource at run-time. We have two kind of renderings; Controller Rendering and View Rendering and am using this inbuilt function:

@Html.Sitecore().Rendering

However this function is only working for Controller Renderings not for View Renderings. Is there a way to solve this problem?

1
  • How do you reference the rendering, by ID or path? if you use path, then try the id of the rendering definition item instead. Also, make sure you have published the rendering definition items if you are viewing the published site. Commented Oct 8, 2017 at 17:21

3 Answers 3

2

I would highly recommend that you use Controller Renderings if you require any sort of logic to calculate the datasource.

That said, the helper does not care if you are using View Renderings or Controller Renderings, just pass the path or the guid of the Rendering Item and Sitecore will handle it as if you had inserted this via the Experience Editor:

@{
    var renderingItem = "rendering-item-guid-or-path"
    var dsItem = "guid-or-path-of-datasource-item"
}
@Html.Sitecore().Rendering(renderingItem, new { DataSource = dsItem })
1

You will need to implement a View Rendering that has a model that inherits from Sitecore.Mvc.Presentation.IRenderingModel. And also implements the Initialize override.

In the initialize you can fetch your custom datasource. See this post for more detail.

Create a new Rendering Model inheriting IRenderingModel.

public class CarouselRenderingModel : Sitecore.Mvc.Presentation.RenderingModel
{
    public override void Initialize(Rendering rendering)
    {
        base.Initialize(rendering);
        CarouselSlides =
            Sitecore.Data.ID.ParseArray(Item["SelectedItems"])
                .Select(id => Item.Database.GetItem(id)).ToList();
    }

    public IList CarouselSlides { get; private set; } 
}

Add this model to Sitecore models

You will find these in the content tree at /sitecore/layout/Models.

Add the model to the rendering model field.

This is the model field on the View Rendering item itself.

0

You have got also following method:

@Html.Sitecore().ItemRendering()

It takes as parameter path or ID of Item. To make it works just do following steps:

  1. copy the ID of rendering item
  2. open data source item
  3. paste the ID of rendering inside data source's field "Renderers"

That's all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.