I am trying to create a simple Image controller Rendering and I don't want to add the template interface to my page type, because I want to add the image dynamicaly to my site.

So here my "problems" begin.

I've created a simple Controllerrendering with a viewmodel

public class PageImageViewModel
{
    public HtmlString PageImage { get; set; }
}

as simple Controller call to my ModelBuilder

public ActionResult PageImage()
{
    return View(PageImageModelBuilder.GetPageImageViewModel(RenderingContext.Current.Rendering.Item));
}

and the modelbuilder

public PageImageViewModel GetPageImageViewModel(Item currentItem)
{
    var model = new PageImageViewModel();
    model.PageImage = new HtmlString(FieldRenderer.Render(currentItem, Templates.PageImage.Fields.Image_FieldName));
    return model;
}

and the view

@using Bazar.Feature.PageContent
@model Bazar.Feature.PageContent.Models.Models.PageImageViewModel

<div class="">
    <p>Hallo Welt</p>
     @Model.PageImage
</div>

thats the codeside I think that should work.

and now the sitecore elments

  • I've created a Interface Template for my _PageImage
  • I've created the Controller rendering

enter image description here

enter image description here

  • And I've tried to add this new control to my "Home" view where I've some dynamic placeholders but I don't get the modal where I can choose what Image I want to see.
  • try to add the controller rendering to my page

enter image description here

  • but after that no image selection is opened and the final layout looks like

enter image description here

  • and I can't edit or select the Image enter image description here

  • so far I understand my Problem is the controller rendering has no connection to my Interface Template _PageImage but I don't understand how to get this "connection"?

up vote 7 down vote accepted

The fix

You need to specify a template in the Datasource Template field of your controller rendering. This is the field that tells Sitecore to prompt an editor to select a data source when adding a rendering.

Image of Datasource Location and Datasource Template fields

A cool feature

Sitecore will take template inheritance into account when allowing you to select data sources, which means that you'll be able to select any item that has a base template of the one you select here (e.g. _PageImage).

Another recommendation

Additionally, it's good practice to specify the location under which data sources can be found by putting that path in the Datasource Location field:

enter image description here

  • When this is the simple fix, what would be the right approach or is there no right or wrong – squadwuschel Oct 14 at 6:08
  • 1
    This is the right approach. Sorry if my wording was unclear. I'll remove the "simple." – Dan Sinclair Oct 14 at 13:47

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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