Component builder for: List Card

Error executing template "Designs/Swift/Paragraph/COMM_InstructorSummary.cshtml"
System.NullReferenceException: Object reference not set to an instance of an object.
   at Dynamicweb.Ecommerce.ProductCatalog.ProductViewModelExtensions.GetProductLink(ProductViewModel product, Int32 pageId, Boolean usePrimaryGroupPageId)
   at CompiledRazorTemplates.Dynamic.RazorEngine_27392d13471a41508b4490f68655aaa9.<>c__DisplayClass3_0.<RenderBody>b0(TextWriter razor_helper_writer) in C:\inetpub\wwwroot\directions2023\Files\Templates\Designs\Swift\Paragraph\COMM_InstructorSummary.cshtml:line 144
   at CompiledRazorTemplates.Dynamic.RazorEngine_27392d13471a41508b4490f68655aaa9.Execute() in C:\inetpub\wwwroot\directions2023\Files\Templates\Designs\Swift\Paragraph\COMM_InstructorSummary.cshtml:line 139
   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> 2 @using Dynamicweb.Ecommerce.ProductCatalog 3 @using Dynamicweb.Frontend 4 @functions{ 5 6 public class ProductInstructor 7 { 8 public string instructorName {get; set;} 9 public string instructorDescription {get; set;} 10 public string instructorLinkedInLink {get; set;} 11 public string instructorImage {get; set;} 12 public string instructorLink {get; set;} 13 public string productPrice {get; set;} 14 public string productSubtitle {get; set;} 15 public string productStaticPrice {get; set;} 16 public string productCurrency {get; set;} 17 public string productCategory {get; set;} 18 public string eventCode {get; set;} 19 } 20 21 static string GetFileExtensionUsingSplit(string filePath) 22 { 23 // Use Split to get the file name and then Split again to get the extension 24 string[] parts = filePath.Split('/'); 25 string fileName = parts[parts.Length - 1]; 26 27 string[] fileNameParts = fileName.Split('.'); 28 string extension = fileNameParts.Length > 1 ? fileNameParts[fileNameParts.Length - 1] : string.Empty; 29 30 return extension; 31 } 32 ProductViewModel product = null; 33 } 34 @{ 35 36 if (Dynamicweb.Context.Current.Items.Contains("ProductDetails")) 37 { 38 product = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"]; 39 } 40 else if (Pageview.Page.Item["DummyProduct"] != null && Pageview.IsVisualEditorMode) 41 { 42 var pageViewModel = Dynamicweb.Frontend.ContentViewModelFactory.CreatePageInfoViewModel(Pageview.Page); 43 ProductListViewModel productList = pageViewModel.Item.GetValue("DummyProduct") != null ? pageViewModel.Item.GetValue("DummyProduct") as ProductListViewModel : new ProductListViewModel(); 44 45 if (productList?.Products is object) 46 { 47 product = productList.Products[0]; 48 } 49 } 50 51 ProductInstructor instructor = new ProductInstructor(); 52 string eventCode = ""; 53 if (product is object) { 54 string selectedDisplayGroup = "Instructor"; 55 56 IEnumerable<string> selectedDisplayGroupIds = selectedDisplayGroup.Split(',').ToList(); 57 List<CategoryFieldViewModel> displayGroups = new List<CategoryFieldViewModel>(); 58 foreach (var selection in selectedDisplayGroupIds) 59 { 60 foreach (CategoryFieldViewModel group in product.FieldDisplayGroups.Values) 61 { 62 if (selection == group.Id) 63 { 64 foreach (var field in group.Fields) 65 { 66 string fieldSystemName = field.Value.SystemName; 67 var fieldValue = field.Value.Value; 68 69 70 switch(fieldSystemName) 71 { 72 case "PersonLink": 73 instructor.instructorLink = fieldValue.ToString(); 74 break; 75 case "PersonDescription": 76 instructor.instructorDescription = fieldValue.ToString(); 77 break; 78 case "PersonImage": 79 instructor.instructorImage = fieldValue.ToString(); 80 break; 81 case "PersonName": 82 instructor.instructorName = fieldValue.ToString(); 83 break; 84 case "PersonLinkedIn": 85 instructor.instructorLinkedInLink = fieldValue.ToString(); 86 break; 87 case "EventCode": 88 instructor.eventCode = fieldValue.ToString(); 89 break; 90 case "ProductPrice": 91 instructor.productPrice = fieldValue.ToString(); 92 break; 93 case "ProductSubtitle": 94 instructor.productSubtitle = fieldValue.ToString(); 95 break; 96 case "ProductStaticPrice": 97 instructor.productStaticPrice = fieldValue.ToString(); 98 break; 99 case "ProductCurrency": 100 instructor.productCurrency = fieldValue.ToString(); 101 break; 102 case "ItemCategory": 103 instructor.productCategory = fieldValue.ToString(); 104 break; 105 } 106 } 107 } 108 } 109 } 110 } 111 } 112 113 114 @{ 115 string layout = Model.Item.GetRawValueString("Layout", "text-start"); 116 layout = layout == "alignLeft" ? "align-items-start text-start" : layout; 117 layout = layout == "alignCenter" ? "align-items-center text-center" : layout; 118 layout = layout == "alignRight" ? "align-items-end text-end" : layout; 119 layout = layout == "alignLeftMiddle" ? "align-items-start text-start justify-content-center" : layout; 120 121 string contentPadding = Model.Item.GetRawValueString("ContentPadding", ""); 122 contentPadding = contentPadding == "none" ? "p-0" : contentPadding; 123 contentPadding = contentPadding == "small" ? "p-3 p-md-3" : contentPadding; 124 contentPadding = contentPadding == "large" ? "p-5 p-md-5" : contentPadding; 125 126 string maxWidth = Model.Item.GetRawValueString("TextReadability", "max-width-on"); 127 maxWidth = maxWidth == "max-width-on" ? " mw-75ch" : maxWidth; 128 maxWidth = maxWidth == "max-width-off" ? " w-100" : maxWidth; @* CUSTOM CODE *@ 129 130 string theme = !string.IsNullOrWhiteSpace(Model.Item.GetRawValueString("Theme")) && Model.Item.GetRawValueString("Theme") != "no-theme" ? " theme " + Model.Item.GetRawValueString("Theme").Replace(" ", "").Trim().ToLower() : ""; 131 } 132 <style> 133 .item_swift_productheader{ 134 min-height:80px; 135 } 136 </style> 137 <div class="h-100 position-relative d-flex flex-column mb-0-last-child @contentPadding @(layout)@(theme) item_@Model.Item.SystemName.ToLower()"> 138 <a name="@Model.ID" class="user-select-none position-absolute top-0" style="scroll-margin-top:var(--header-height,150px)"></a> 139 @{@RenderBody(maxWidth, instructor)} 140 </div> 141 142 @helper RenderBody(string maxWidth, ProductInstructor instructor) 143 { 144 string link = product.GetProductLink(GetPageIdByNavigationTag("Shop"), false); 145 Dynamicweb.Frontend.LinkViewModel link1 = new Dynamicweb.Frontend.LinkViewModel(); 146 Dynamicweb.Frontend.LinkViewModel link2 = new Dynamicweb.Frontend.LinkViewModel(); 147 148 int ticketsPageId = GetPageIdByNavigationTag("Tickets"); 149 int sponsorshipPageId = GetPageIdByNavigationTag("Sponsorships"); 150 //string ticketsEventCode = Model.Item.GetString("Ticket_Event_Code"); 151 //string sponsorshipEventCode = Model.Item.GetString("Sponsorship_Event_Code"); 152 153 //ticketsEventCode = ticketsEventCode != "" ? ticketsEventCode : parentEventCode; 154 //sponsorshipEventCode = sponsorshipEventCode != "" ? sponsorshipEventCode : parentEventCode; 155 string buyLink = "Default.aspx?ID="; 156 buyLink = instructor.productCategory == "SPONSOR" || instructor.productCategory == "S-RELATED" ? buyLink + sponsorshipPageId : buyLink + ticketsPageId; 157 buyLink = instructor.eventCode != "" ? buyLink + "&eventCode=" + instructor.eventCode : buyLink; 158 159 160 @*if(linkType1 == "buy-tickets") 161 { 162 LinkViewModel ticketsLink = new LinkViewModel(); 163 ticketsLink.IsExternal = false; 164 ticketsLink.Url = "/Default.aspx?ID=" + ticketsPageId; 165 ticketsLink.Url = ticketsEventCode != "" ? ticketsLink.Url + "&EventCode=" + ticketsEventCode : ticketsLink.Url; 166 link1 = ticketsLink; 167 } 168 169 if(linkType1 == "buy-sponsorship") 170 { 171 LinkViewModel sponsorshipLink = new LinkViewModel(); 172 sponsorshipLink.IsExternal = false; 173 sponsorshipLink.Url = "/Default.aspx?ID=" + sponsorshipPageId; 174 sponsorshipLink.Url = sponsorshipEventCode != "" ? sponsorshipLink.Url + "&EventCode=" + sponsorshipEventCode : sponsorshipLink.Url; 175 link1 = sponsorshipLink; 176 } 177 *@ 178 179 @*if (!string.IsNullOrEmpty(instructor.productSubtitle)) 180 { 181 string titleColor = "text-inherit"; 182 string titleOpacity = "opacity-100"; 183 string titleFontSize = "h4"; 184 string headingLevel = "h2"; 185 string headingLevelStart = $"<{headingLevel} class=\"{titleFontSize} {titleColor} {titleOpacity}{maxWidth} mb-3\">"; 186 string headingLevelStop = $"</{headingLevel}>"; 187 188 @headingLevelStart 189 @instructor.productSubtitle 190 @headingLevelStop 191 }*@ 192 193 @*if (!string.IsNullOrEmpty(instructor.instructorName)) 194 { 195 string subtitleColor = "text-inherit"; 196 string subtitleOpacity = "opacity-100"; 197 string subtitleFontSize = "fs-6"; 198 199 <p class="@subtitleFontSize @subtitleColor @subtitleOpacity mb-3@(maxWidth)"> 200 <strong>@(Translate("Instructor") + ": " + instructor.instructorName)</strong> 201 </p> 202 }*@ 203 string productStaticPrice = instructor.productStaticPrice != "" ? instructor.productStaticPrice : instructor.productPrice + " " + instructor.productCurrency; 204 205 if (!string.IsNullOrEmpty(productStaticPrice)) 206 { 207 string titleColor = "text-inherit"; 208 string titleOpacity = "opacity-100"; 209 string titleFontSize = "h4"; 210 string headingLevel = "h4"; 211 string headingLevelStart = $"<{headingLevel} class=\"{titleFontSize} {titleColor} {titleOpacity}{maxWidth} mb-3\">"; 212 string headingLevelStop = $"</{headingLevel}>"; 213 214 @headingLevelStart 215 @(Translate("Price")+":"+ productStaticPrice) 216 @headingLevelStop 217 } 218 219 220 if (!string.IsNullOrEmpty(Model.Item.GetString("Title")) && !Model.Item.GetBoolean("HideTitle")) 221 { 222 string titleColor = "text-inherit"; 223 string titleOpacity = "opacity-100"; 224 string titleFontSize = "h5"; 225 string headingLevel = "h5"; 226 string headingLevelStart = $"<{headingLevel} class=\"{titleFontSize} {titleColor} {titleOpacity}{maxWidth} mb-3\">"; 227 string headingLevelStop = $"</{headingLevel}>"; 228 229 @headingLevelStart 230 @Model.Item.GetString("Title") 231 @headingLevelStop 232 } 233 234 235 236 <div class="py-2" data-col-size="12"> 237 238 <div class="d-flex flex-row w-100 mb-2"> 239 <div class="input-group input-primary-button-group flex-nowrap"> 240 <a href="@buyLink" class="btn btn-primary btn-sm" style="white-space: nowrap" title="Translate("Add to cart")" id="AddToCartButton@product.Id"> 241 <span class="text-nowrap d-flex align-items-center justify-content-center gap-2"> 242 <span class="icon-2"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather feather-shopping-cart"><circle cx="9" cy="21" r="1"></circle><circle cx="20" cy="21" r="1"></circle><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path></svg></span> <span class="d-none d-md-inline">@Translate("Buy this product")</span><span class="d-inline d-md-none">@Translate("Buy")</span> 243 </span> 244 </a> 245 </div> 246 </div> 247 248 249 <div class="d-flex flex-row w-100"> 250 <div class="input-group input-primary-button-group flex-nowrap"> 251 <a href="@link" class="btn btn-secondary btn-sm" style="white-space: nowrap" title="@Translate("More details")" id="ViewButton@product.Id"> 252 <span class="text-nowrap d-flex align-items-center justify-content-center gap-2"> 253 <span class="d-none d-md-inline">@Translate("More details")</span><span class="d-inline d-md-none">@Translate("More details")</span> 254 </span> 255 </a> 256 </div> 257 </div> 258 </div> 259 } 260 261 262 @helper RenderImage(ProductInstructor instructor) 263 { 264 FileViewModel image = new FileViewModel(); 265 image.Extension = GetFileExtensionUsingSplit(instructor.instructorImage); 266 image.Name = instructor.instructorName; 267 image.Path = instructor.instructorImage; 268 image.FocalY = 0; 269 image.FocalX = 0; 270 271 if (!string.IsNullOrEmpty(instructor.instructorImage)) 272 { 273 int xPos = 50; 274 int yPos = 50; 275 string ratioCssClass = string.Empty;//Model.Item.GetRawValueString("ImageAspectRatio") != "0" && Model.Item.GetRawValueString("ImageAspectRatio") != "" ? "ratio" : string.Empty; 276 string ratioVariable = string.Empty;//Model.Item.GetRawValueString("ImageAspectRatio") != "0" && Model.Item.GetRawValueString("ImageAspectRatio") != "" ? "style=\"--bs-aspect-ratio: " + Model.Item.GetRawValueString("ImageAspectRatio") + "\"" : string.Empty; 277 string imageAspect = string.Empty;//Model.Item.GetRawValueString("ImageAspectRatio", string.Empty); 278 string ImageObjectFit = string.Empty;//Model.Item.GetRawValueString("ImageObjectFit", string.Empty); 279 ImageObjectFit = ImageObjectFit == "cover" ? string.Empty : ImageObjectFit; 280 ImageObjectFit = ImageObjectFit == "contain" ? "object-fit:contain" : ImageObjectFit; 281 282 var parms = new Dictionary<string, object>(); 283 parms.Add("loading", "lazy"); 284 parms.Add("style", ImageObjectFit); 285 parms.Add("alt", instructor.instructorName); 286 parms.Add("columns", 1); 287 //parms.Add("cssClass", "rounded-7"); @* CUSTOM CODE *@ 288 289 290 parms.Add("fullwidth", true); 291 parms.Add("cssClass", "img-fluid"); 292 293 294 295 <figure> 296 @RenderPartial("../Components/Image.cshtml", image ?? new Dynamicweb.Frontend.FileViewModel(), parms) 297 </figure> 298 299 300 301 } 302 }