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
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?