Hi Everyone!
Recently I had to fix an issue in a breadcrumb component, it took some minutes to understand what was wrong with the Glass Mapper.
Situation: the following code was always printing repeated links, JUST FOR PREVIEW MODE ! , for Experience Mode, it was working just fine
}
@for (var i = startLevelCounter; i <= ancestorDepth + 1; i++)
{
var levelItem = currentItem.GetAncestorOrSelfOfLevel(i, rootItem.Item);
var breadcrumb = basePageRepository.GetBasePage(levelItem);
@Editable(x, x => x.BreadcrumbTitle)
The output on Experience Editor: “Home -> Products”
The output on Preview Mode: “Home -> Products”
At first glance, the solution, the solution that comes to my mind, was to adjust the code regarding the situation, so instead of @Editable(x, x => x.BreadcrumbTitle)
We could simply use :
if (Sitecore.Context.PageMode.IsExperienceEditor)
{ @Editable(x, x => x.BreadcrumbTitle) }
else { @breadcrumb .BreadcrumbTitle }
Indeed this solution would work, but if you look carefully, we may see a mistake in the implementation of glass mapper!.
Before :
@Editable(x, x => x.BreadcrumbTitle)
The solution: Pass the correct object(breadcrumb) to the context !!how ?
@Editable(breadcrumb, x => x.BreadcrumbTitle)
In this case, we must remember that the loop will change the context, and the first parameter, the x, will need to be the object !