Error executing template "Designs/Swift/eCom/ProductCatalog/ProductSearchDropdownResponse.cshtml"
System.NullReferenceException: Object reference not set to an instance of an object.
   at CompiledRazorTemplates.Dynamic.RazorEngine_07ea7d7e333443d08b621ca81418b7f4.Execute() in E:\Dynamicweb.NET\Solutions\swift.blomberg.espresso4.dk\Files\Templates\Designs\Swift\eCom\ProductCatalog\ProductSearchDropdownResponse.cshtml:line 190
   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 ViewModelTemplate<ProductListViewModel> 2 @using Dynamicweb.Rendering 3 @using Dynamicweb.Ecommerce.ProductCatalog 4 5 @{ 6 string searchTerm = string.Empty; 7 string layout = Dynamicweb.Context.Current.Request["SearchLayout"] != null ? Dynamicweb.Context.Current.Request["SearchLayout"].ToString() : "field"; 8 string itemPadding = layout == "icon" ? "px-1" : "px-1"; 9 string iconPath = "/Files/Templates/Designs/Swift/Assets/icons/"; 10 11 if (!string.IsNullOrEmpty(Dynamicweb.Context.Current.Request["eq"])) 12 { 13 searchTerm = System.Net.WebUtility.HtmlEncode(Dynamicweb.Context.Current.Request["eq"].Trim()); 14 } 15 Dictionary<string, string> suggestions = new Dictionary<string, string>(); 16 17 } 18 19 @functions{ 20 string Highlight(string input, string textToHighlight) 21 { 22 if (string.IsNullOrEmpty(textToHighlight)) 23 { 24 return input; 25 } 26 input = fixHtmlEncoding(input.ToLower()); 27 foreach (string term in textToHighlight.Split(new[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries)) 28 { 29 input = input.Replace(term, "</mark>" + term + "<mark>"); 30 } 31 32 return "<span class=\"js-suggestion flex-fill text-break\" data-suggestion-value=\"\"><mark>" + input + "</mark></span>"; 33 } 34 35 string ProductNameHighlight(string input, string textToHighlight) 36 { 37 if (string.IsNullOrEmpty(textToHighlight)) 38 { 39 return input; 40 } 41 input = fixHtmlEncoding(input.ToLower()); 42 foreach (string term in textToHighlight.Split(new[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries)) 43 { 44 input = input.Replace(term, "<mark>" + term + "</mark>"); 45 } 46 47 return "<span class=\"js-suggestion flex-fill text-break\" data-suggestion-value=\"\">" + input + "</span>"; 48 } 49 50 string fixHtmlEncoding(string input) 51 { 52 if (input.IndexOf("&") > 0) 53 { 54 if (input.IndexOf(" ", input.IndexOf("&")) > 0) 55 { 56 input = input.Replace(" ", "; "); 57 } 58 else 59 { 60 input += ";"; 61 input = System.Net.WebUtility.HtmlDecode(input); 62 } 63 } 64 return input; 65 } 66 67 string escape(string input) 68 { 69 return Dynamicweb.Core.Encoders.HtmlEncoder.JavaScriptStringEncode(fixHtmlEncoding(input)); 70 } 71 72 void AddSuggestion(string suggestion, string group, Dictionary<string, string> suggestions) 73 { 74 suggestion = suggestion.ToLower(); 75 if (!suggestions.ContainsKey(suggestion)) 76 { 77 suggestions.Add(suggestion, group); 78 } 79 } 80 } 81 82 @if (Model.SpellCheckerSuggestions != null && Model.SpellCheckerSuggestions.Any()) 83 { 84 List<string> validSuggestions = new List<string>(); 85 foreach (string suggestion in Model.SpellCheckerSuggestions) 86 { 87 if (!validSuggestions.Contains(suggestion)) 88 { 89 validSuggestions.Add(suggestion); 90 } 91 } 92 if (validSuggestions.Count == 0) 93 { 94 foreach (string suggestion in Model.SpellCheckerSuggestions) 95 { 96 if (!validSuggestions.Contains(suggestion)) 97 { 98 validSuggestions.Add(suggestion); 99 } 100 } 101 } 102 if (validSuggestions.Count > 0) 103 { 104 foreach (string suggestion in validSuggestions.Take(5)) 105 { 106 AddSuggestion(suggestion, "Did you mean", suggestions); 107 <li class="dropdown-item d-flex flex-row align-items-center py-2 px-0 text-wrap"> 108 109 <span class="dropdown-item-search-icon d-flex align-items-center justify-content-center" aria-hidden="true" onclick="swift.Typeahead.selectSuggestion(this.parentElement);"></span> 110 111 <span class="flex-fill" onclick="swift.Typeahead.selectSuggestion(this.parentElement);">@Highlight(suggestion, searchTerm)</span> 112 113 <span class="dropdown-item-arrow-icon d-flex d-lg-none align-items-center justify-content-center" aria-hidden="true" onclick="swift.Typeahead.setSuggestion(this.parentElement);"></span> 114 115 </li> 116 } 117 } 118 } 119 120 @if (Model.FacetGroups.Any()) 121 { 122 var maxSuggestions = 5; 123 foreach (var facetGroup in Model.FacetGroups) 124 { 125 foreach (var facetItem in facetGroup.Facets) 126 { 127 var facetOptionCount = facetItem.OptionCount; 128 var optionActiveCount = facetItem.OptionActiveCount; 129 var facetOptionResultTotalCount = facetItem.OptionResultTotalCount; 130 var queryParameter = facetItem.QueryParameter; 131 var template = facetItem.RenderType; 132 133 var maxValue = facetItem.MaximumFacetValue; 134 var minValue = facetItem.MinimumFacetValue; 135 136 List<FacetOptionViewModel> validOptions = new List<FacetOptionViewModel>(); 137 138 foreach (var facetOption in facetItem.Options.OrderByDescending(option => option.Count)) 139 { 140 if (searchTerm != null && validOptions.Count < maxSuggestions) 141 { 142 validOptions.Add(facetOption); 143 } 144 } 145 146 if (validOptions.Count > 0) 147 { 148 <li class="text-wrap"> 149 <h6 class="dropdown-header ps-5 pe-3 py-3">@facetItem.Name</h6> 150 </li> 151 foreach (var facetOption in validOptions) 152 { 153 AddSuggestion(facetOption.Label, facetItem.Name, suggestions); 154 <li class="dropdown-item @itemPadding" onclick="swift.Typeahead.selectSuggestion(this);" data-param="@queryParameter" data-paramvalue="@facetOption.Value"> 155 @Highlight(facetOption.Label, searchTerm) 156 <div class="">@facetOption.Count</div> 157 </li> 158 } 159 } 160 161 } 162 } 163 } 164 165 @if (Model.Products.Count() > 0) 166 { 167 var validSuggestions = new List<ProductViewModel>(); 168 string label = Translate("Products"); 169 if (Model.Group != null) 170 { 171 label = Model.Group.Name; 172 } 173 174 foreach (var product in Model.Products) 175 { 176 if (searchTerm != null && product.Name.IndexOf(searchTerm, StringComparison.InvariantCultureIgnoreCase) >= 0 || true) 177 { 178 AddSuggestion(product.Name, label, suggestions); 179 validSuggestions.Add(product); 180 } 181 } 182 if (validSuggestions.Count > 0) 183 { 184 <li class="text-wrap"> 185 <h6 class="dropdown-header ps-5 pe-3 py-3">@label</h6> 186 </li> 187 foreach (var suggestion in validSuggestions) 188 { 189 var defaultGroupId = suggestion.PrimaryOrDefaultGroup.Id; 190 var selectedDetailsPage = Dynamicweb.Ecommerce.Services.ProductGroups.GetGroup(defaultGroupId).Meta.PrimaryPage; 191 selectedDetailsPage = !string.IsNullOrEmpty(selectedDetailsPage) ? "data-selected-details-page=\"" + Dynamicweb.Frontend.SearchEngineFriendlyURLs.GetFriendlyUrl(selectedDetailsPage) + "\"" : ""; 192 193 <li class="dropdown-item d-flex flex-row flex-lg-row-reverse align-items-center text-wrap px-0 text-wrap" onclick="swift.Typeahead.selectSuggestion(this);" data-param="ProductId" @selectedDetailsPage data-paramvalue="@suggestion.Id"> 194 195 <span aria-hidden="true" class="d-lg-none dropdown-item-search-icon d-flex align-items-center justify-content-center"></span> 196 @ProductNameHighlight(suggestion.Name + " - <small class=\"fs-8\">" + suggestion.Number + "</small>", searchTerm) 197 <img class="mx-2" src="/admin/public/getimage.ashx?image=@Dynamicweb.Context.Current.Server.UrlEncode(suggestion.DefaultImage.Value)&width=32&height=32&format=webp&Crop=5&fillcanvas=true" height="32" width="32" alt="@suggestion.Name"> 198 </li> 199 } 200 } 201 } 202 203 @if (!string.IsNullOrEmpty(searchTerm)) 204 { 205 if (Model.TotalProductsCount > 0) 206 { 207 <li class="dropdown-item text-center text-decoration-underline text-wrap @itemPadding" onclick="swift.Typeahead.selectSuggestion(this);"> 208 @Translate("View all") @Model.TotalProductsCount @Translate("products for")<mark> "<span class="js-suggestion">@searchTerm</span>"</mark> 209 </li> 210 } 211 else 212 { 213 <li class="dropdown-item text-center text-decoration-underline text-wrap @itemPadding" onclick="swift.Typeahead.selectSuggestion(this);" style="cursor: pointer;"> 214 @Translate("We have no results for")<mark> "<span class="js-suggestion">@searchTerm</span>"</mark> 215 </li> 216 } 217 <li class="py-2"></li> 218 } 219