Error executing template "Designs/Swift/Paragraph/Swift_ProductDefaultImage.cshtml"
System.NullReferenceException: Object reference not set to an instance of an object.
at CompiledRazorTemplates.Dynamic.RazorEngine_093de8c1eaf949ffa39e1c08895c307c.Execute() in D:\dynamicweb.net\Solutions\Mennt\mennt.cloud.dynamicweb-cms.com\Files\Templates\Designs\Swift\Paragraph\Swift_ProductDefaultImage.cshtml:line 33
at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context, TextWriter reader)
at RazorEngine.Templating.RazorEngineService.RunCompile(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
at RazorEngine.Templating.RazorEngineServiceExtensions.<>c__DisplayClass16_0.<RunCompile>b__0(TextWriter writer)
at RazorEngine.Templating.RazorEngineServiceExtensions.WithWriter(Action`1 withWriter)
at Dynamicweb.Rendering.RazorTemplateRenderingProvider.Render(Template template)
at Dynamicweb.Rendering.TemplateRenderingService.Render(Template template)
at Dynamicweb.Rendering.Template.RenderRazorTemplate()
1 @inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.ParagraphViewModel>
@using Dynamicweb.Ecommerce.ProductCatalog
@using Dynamicweb.Ecommerce.CustomerExperienceCenter.Favorites
@{
ProductViewModel product = new ProductViewModel();
ProductViewModelSettings productSetting = new ProductViewModelSettings
{
LanguageId = Dynamicweb.Ecommerce.Common.Context.LanguageID,
CurrencyCode = Dynamicweb.Ecommerce.Common.Context.Currency.Code,
CountryCode = Dynamicweb.Ecommerce.Common.Context.Country.Code2,
ShopId = Pageview.Area.EcomShopId
};
if (Dynamicweb.Context.Current.Items.Contains("ProductDetails"))
{
product = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"];
}
else if (Pageview.Item["DummyProduct"] != null)
{
string dummyProductId = "";
var pageViewModel = Dynamicweb.Frontend.ContentViewModelFactory.CreatePageInfoViewModel(Pageview.Page);
ProductListViewModel productList = pageViewModel.Item.GetValue("DummyProduct") != null ? pageViewModel.Item.GetValue("DummyProduct") as ProductListViewModel : new ProductListViewModel();
if (productList.Products != null)
{
foreach (var p in productList.Products) { dummyProductId = p.Id; }
ProductViewModel dummyProduct = dummyProductId != "" ? ViewModelFactory.CreateView(productSetting, dummyProductId) : new ProductViewModel();
product = dummyProduct;
}
else
{
product = ViewModelFactory.CreateView(productSetting, Dynamicweb.Ecommerce.Services.Products.GetLastActiveProducts(1, Dynamicweb.Ecommerce.Common.Context.LanguageID, false).FirstOrDefault().Id);
}
}
else if (Pageview.Item["DummyProduct"] == null)
{
product = ViewModelFactory.CreateView(productSetting, Dynamicweb.Ecommerce.Services.Products.GetLastActiveProducts(1, Dynamicweb.Ecommerce.Common.Context.LanguageID, false).FirstOrDefault().Id);
}
string imagePath = product?.DefaultImage?.Value ?? "";
imagePath = Dynamicweb.Context.Current.Server.UrlEncode(imagePath);
string ratio = Model.Item.GetRawValueString("ImageAspectRatio", "");
ratio = ratio != "0" ? ratio : "";
string ratioCssClass = ratio != "" ? " ratio" : "";
string ratioVariable = ratio != "" ? "--bs-aspect-ratio: " + ratio : "";
string width = Model.Item.GetRawValueString("Width", "auto");
int smallImageSize = 640;
int largeImageSize = width == "auto" ? 1280 : Convert.ToInt32(width);
string imagePathXs = "/Admin/Public/GetImage.ashx?width=" + smallImageSize + "&image=" + imagePath + "&format=webp";
string imagePathS = "/Admin/Public/GetImage.ashx?width=" + largeImageSize + "&image=" + imagePath + "&format=webp";
string imagePathFallBack = "/Admin/Public/GetImage.ashx?width=" + largeImageSize + "&image=" + imagePath + "&format=webp";
var badgeParms = new Dictionary<string, object>();
badgeParms.Add("size", "h7");
badgeParms.Add("saleBadgeType", Model.Item.GetRawValue("SaleBadgeType"));
badgeParms.Add("saleBadgeCssClassName", Model.Item.GetRawValue("SaleBadgeDesign"));
badgeParms.Add("newBadgeCssClassName", Model.Item.GetRawValue("NewBadgeDesign"));
badgeParms.Add("newPublicationDays", Model.Item.GetInt32("NewPublicationDays"));
badgeParms.Add("campaignBadgesValues", Model.Item.GetRawValueString("CampaignBadges"));
string badgeSize = Model.Item.GetRawValueString("BadgeSize", "fs-2");
bool showFavoritesSelector = !string.IsNullOrEmpty(Model.Item.GetString("ShowAddToFavorites")) ? Model.Item.GetBoolean("ShowAddToFavorites") : false;
string anonymousUsersLimitations = Pageview.AreaSettings.GetRawValueString("AnonymousUsers", "");
bool anonymousUser = Pageview.User == null;
var favoriteParameters = new Dictionary<string, object>();
if (!anonymousUser && showFavoritesSelector)
{
int defaultFavoriteListId = 0;
IEnumerable<FavoriteList> favoreiteLists = Pageview.User.GetFavoriteLists();
if (favoreiteLists.Count() == 1)
{
foreach (FavoriteList list in favoreiteLists)
{
defaultFavoriteListId = list.ListId;
}
}
favoriteParameters.Add("ListId", defaultFavoriteListId);
}
var selectedImageCategories = Model.Item.GetRawValueString("GetAlternativeImageFrom").Split(',').ToList();
List<MediaViewModel> allAssetsImages = product.AssetCategories.Where(x => selectedImageCategories.Contains(x.SystemName)).SelectMany(x => x.Assets).ToList();
allAssetsImages.RemoveAll(x => x.Value == product.DefaultImage.Value);
MediaViewModel alternativeImage = allAssetsImages.FirstOrDefault();
string altImagePathXs = string.Empty;
string altImagePathS = string.Empty;
if (!string.IsNullOrEmpty(alternativeImage?.Value))
{
altImagePathXs = "/Admin/Public/GetImage.ashx?width=" + smallImageSize + "&image=" + Dynamicweb.Context.Current.Server.UrlEncode(alternativeImage.Value) + "&format=webp";
altImagePathS = "/Admin/Public/GetImage.ashx?width=" + largeImageSize + "&image=" + Dynamicweb.Context.Current.Server.UrlEncode(alternativeImage.Value) + "&format=webp";
}
@* Theme settings *@
string theme = !string.IsNullOrWhiteSpace(Model.Item.GetRawValueString("Theme")) ? " theme " + Model.Item.GetRawValueString("Theme").Replace(" ", "").Trim().ToLower() : "";
string themeBorder = !string.IsNullOrWhiteSpace(theme) ? "border: 1px solid rgba(0,0,0,0)" : "";
string themePadding = !string.IsNullOrWhiteSpace(theme) ? "p-2 p-lg-3" : "";
}
@if (product.Id != null)
{
string imageId = "ProductImage_" + product.Id + product.VariantId;
bool saleBadgeEnabled = !string.IsNullOrWhiteSpace(Model.Item.GetRawValueString("SaleBadgeDesign")) && Model.Item.GetRawValueString("SaleBadgeDesign") != "none" ? true : false;
bool newBadgeEnabled = !string.IsNullOrWhiteSpace(Model.Item.GetRawValueString("NewBadgeDesign")) && Model.Item.GetRawValueString("NewBadgeDesign") != "none" ? true : false;
DateTime createdDate = product.Created.Value;
bool showBadges = saleBadgeEnabled && product.Discount.Price != 0 ? true : false;
showBadges = (newBadgeEnabled && Model.Item.GetInt32("NewPublicationDays") == 0) || (newBadgeEnabled && (createdDate.AddDays(Model.Item.GetInt32("NewPublicationDays")) > DateTime.Now)) ? true : showBadges;
showBadges = !string.IsNullOrEmpty(Model.Item.GetRawValueString("CampaignBadges")) ? true : showBadges;
string fullWidth = width == "auto" ? "w-100" : "";
string customWidth = width != "auto" ? "style=\"width: " + width + "px\"" : "style=\"min-width: 60px\"";
if (!string.IsNullOrEmpty(imagePath))
{
<div class="h-100 @fullWidth @theme position-relative item_@Model.Item.SystemName.ToLower()" @customWidth>
<div class="ratio" style="@(ratioVariable)">
<div class="d-flex justify-content-center align-items-center">
<img id="@imageId"
srcset="
@imagePathXs @(smallImageSize)w,
@imagePathS @(largeImageSize)w"
sizes="(min-width: 992px) 33vw, 50vw"
src="@imagePathFallBack"
@if (!string.IsNullOrEmpty(alternativeImage?.Value)) { <text>
data-alternative-image="
@altImagePathXs @(smallImageSize)w,
@altImagePathS @(largeImageSize)w"
onmouseover="swift.Image.swapImage(event)"
onmouseout="swift.Image.swapImage(event)"
</text> }
loading="lazy"
decoding="async"
class="h-100 w-100 @themePadding"
style="object-fit: contain; @themeBorder"
alt="@product.Name">
</div>
</div>
@if (showBadges)
{
<div class="position-absolute top-0 left-0 p-2 p-lg-3 ps-0 ps-lg-0 @badgeSize">
@RenderPartial("Components/EcommerceBadge.cshtml", product, badgeParms)
</div>
}
@if (!anonymousUser)
{
if (showFavoritesSelector)
{
<div class="position-absolute top-0 end-0 my-3" style="z-index: 2">
@RenderPartial("Components/ToggleFavorite.cshtml", product, favoriteParameters)
</div>
}
}
</div>
}
}
else if (Pageview.IsVisualEditorMode)
{
<div class="alert alert-dark m-0">@Translate("No products available")</div>
}