0

I am trying to create a controller Rendering with the use of FieldRenderer.Render, but this does not work.

The Controller

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

the function where I am building my ViewModel

   public PageImageViewModel GetPageImageViewModel(Item currentItem)
    {
        var model = new PageImageViewModel();
        try
        {
            var renderer = new FieldRenderer();
            renderer.DisableWebEditing = false;
            renderer.Item = currentItem;
            renderer.FieldName = "Image";

            model.PageImage = new HtmlString(renderer.Render());
            //model.PageImage = new HtmlString(FieldRenderer.Render(currentItem, "Image"));
            model.Beschreibung = new HtmlString(FieldRenderer.Render(currentItem, Templates.PageImage.Fields.Beschreibung_FieldName));
        }
        catch (Exception exception)
        {
            Sitecore.Diagnostics.Log.Error("GetPageImageViewModel Error", exception, typeof(PageImageViewModel));
        }

        return model;
    }

the view Model

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

and last but not Least the Controller Rendering

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

<div class="">
  <p>Hallo Welt</p>
  @Model.PageImage
  <p>Sitecore()Field:</p>
  @Html.Sitecore().Field("Image", new { mw = 400, @class = "img-fluid" })
  <p>FieldRenderer: 
     @Model.Beschreibung
  </p>
  <p>Default Field: @Html.Sitecore().Field("Beschreibung")</p>
 </div>

The Renderings with "Sitecore().Field" are working, but my controller Renderings with HTmlString are not showing up

enter image description here

I think I am doing something wrong with the Context perhaps but I am not shure what. When you need more informations then plz tell me.

Is using a ViewModel with HtmlString the right approach or what should I use?

1 Answer 1

2

It seems that I was using the wrong Context Item when I use this

RenderingContext.Current.Rendering.Item

everything is working fine. Would be nice to know what is the difference between these two contexts

2
  • 1
    brainjocks.wpengine.com/sitecore-mvc-item-maze might help you a bit..
    – Gatogordo
    Commented Oct 15, 2018 at 12:01
  • 1
    For RenderingContext.Current.Rendering.Item - it uses the datsource of the current, ancestor or page item (if datasource not set). For RenderingContext.Current.ContextItem - uses the current page item as the datasource or the ancestor rendering. It ignores the renderings datasource. There is a setting in Sitecore.MVC.Config that can change the behavior called Mvc.AllowDataSourceNesting.
    – RReilley
    Commented Oct 15, 2018 at 13:42

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.