Error executing template "Designs/Swift/eCom/ProductCatalog/ProductSearchDropdownResponse.cshtml"
System.ArgumentNullException: Value cannot be null.
Parameter name: source
at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
at CompiledRazorTemplates.Dynamic.RazorEngine_106bd877aaf640a2bc7fe00fc3f2198f.Execute() in D:\dynamicweb.net\Solutions\Co3\itools.cloud.dynamicweb-cms.com\files\Templates\Designs\Swift\eCom\ProductCatalog\ProductSearchDropdownResponse.cshtml:line 138
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 = 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 = input.ToLower();
27 input = input.Replace(textToHighlight, "</mark>" + textToHighlight + "<mark>");
28
29 return "<span class=\"js-suggestion flex-fill text-break\" data-suggestion-value=\"\"><mark>" + input + "</mark></span>";
30 }
31
32 string ProductNameHighlight(string input, string textToHighlight)
33 {
34 if (string.IsNullOrEmpty(textToHighlight))
35 {
36 return input;
37 }
38 input = input.ToLower();
39 input = input.Replace(textToHighlight, "</mark>" + textToHighlight + "<mark>");
40
41 return "<span class=\"js-suggestion flex-fill text-break\" data-suggestion-value=\"\">" + input + "</span>";
42 }
43
44 string escape(string input)
45 {
46 return Dynamicweb.Core.Encoders.HtmlEncoder.JavaScriptStringEncode(input);
47 }
48
49 void AddSuggestion(string suggestion, string group, Dictionary<string, string> suggestions)
50 {
51 suggestion = suggestion.ToLower();
52 if (!suggestions.ContainsKey(suggestion))
53 {
54 suggestions.Add(suggestion, group);
55 }
56 }
57 }
58
59 @if (Model.SpellCheckerSuggestions != null && Model.SpellCheckerSuggestions.Any())
60 {
61 List<string> validSuggestions = new List<string>();
62 foreach (string suggestion in Model.SpellCheckerSuggestions)
63 {
64 if (!validSuggestions.Contains(suggestion))
65 {
66 validSuggestions.Add(suggestion);
67 }
68 }
69 if (validSuggestions.Count == 0)
70 {
71 foreach (string suggestion in Model.SpellCheckerSuggestions)
72 {
73 if (!validSuggestions.Contains(suggestion))
74 {
75 validSuggestions.Add(suggestion);
76 }
77 }
78 }
79 if (validSuggestions.Count > 0)
80 {
81 foreach (string suggestion in validSuggestions.Take(5))
82 {
83 AddSuggestion(suggestion, "Did you mean", suggestions);
84 <li class="dropdown-item d-flex flex-row align-items-center py-2 px-0 text-wrap text-decoration-underline-hover">
85 <span class="dropdown-item-search-icon d-flex align-items-center justify-content-center" aria-hidden="true" onclick="swift.Typeahead.selectSuggestion(this.parentElement);"></span>
86 <span class="flex-fill" onclick="swift.Typeahead.selectSuggestion(this.parentElement);">@Highlight(suggestion, searchTerm)</span>
87 <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>
88 </li>
89 }
90 }
91 }
92
93 @if (Model.FacetGroups.Any())
94 {
95 var maxSuggestions = 5;
96 foreach (var facetGroup in Model.FacetGroups)
97 {
98 foreach (var facetItem in facetGroup.Facets)
99 {
100 var facetOptionCount = facetItem.OptionCount;
101 var optionActiveCount = facetItem.OptionActiveCount;
102 var facetOptionResultTotalCount = facetItem.OptionResultTotalCount;
103 var queryParameter = facetItem.QueryParameter;
104 var template = facetItem.RenderType;
105
106 var maxValue = facetItem.MaximumFacetValue;
107 var minValue = facetItem.MinimumFacetValue;
108
109 List<FacetOptionViewModel> validOptions = new List<FacetOptionViewModel>();
110
111 foreach (var facetOption in facetItem.Options.OrderByDescending(option => option.Count))
112 {
113 if (searchTerm != null && validOptions.Count < maxSuggestions)
114 {
115 validOptions.Add(facetOption);
116 }
117 }
118
119 if (validOptions.Count > 0)
120 {
121 <li class="text-wrap">
122 <h6 class="dropdown-header ps-5 pe-3 py-3">@facetItem.Name</h6>
123 </li>
124 foreach (var facetOption in validOptions)
125 {
126 AddSuggestion(facetOption.Label, facetItem.Name, suggestions);
127 <li class="dropdown-item @itemPadding" onclick="swift.Typeahead.selectSuggestion(this);" data-param="@queryParameter" data-paramvalue="@facetOption.Value">
128 @Highlight(facetOption.Label, searchTerm)
129 <div class="">@facetOption.Count</div>
130 </li>
131 }
132 }
133
134 }
135 }
136 }
137
138 @if (Model.Products.Count() > 0)
139 {
140 var validSuggestions = new List<ProductViewModel>();
141 string label = Translate("Products");
142 if (Model.Group != null)
143 {
144 label = Model.Group.Name;
145 }
146
147 foreach (var product in Model.Products)
148 {
149 if (searchTerm != null && product.Name.IndexOf(searchTerm, StringComparison.InvariantCultureIgnoreCase) >= 0 || true)
150 {
151 AddSuggestion(product.Name, label, suggestions);
152 validSuggestions.Add(product);
153 }
154 }
155 if (validSuggestions.Count > 0)
156 {
157 foreach (var suggestion in validSuggestions)
158 {
159 var defaultGroupId = suggestion.PrimaryOrDefaultGroup.Id;
160 var selectedDetailsPage = $"Default.aspx?ID={GetPageIdByNavigationTag("Shop").ToString()}";
161 string productDetailLink = Dynamicweb.Frontend.SearchEngineFriendlyURLs.GetFriendlyUrl($"{selectedDetailsPage}&GroupID={defaultGroupId}&ProductID={suggestion.Id}");
162
163 <li class="dropdown-item d-flex flex-lg-row-reverse align-items-center text-wrap px-0 py-2 text-wrap text-decoration-underline-hover" onclick="swift.Typeahead.selectSuggestion(this);" data-param="ProductId" data-selected-details-page="@productDetailLink" data-paramvalue="@suggestion.Id">
164 <span aria-hidden="true" class="d-lg-none dropdown-item-search-icon d-flex align-items-center justify-content-center"></span>
165 @ProductNameHighlight(suggestion.Name + " - " + suggestion.Number, searchTerm)
166 <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">
167 </li>
168 }
169 }
170 }
171
172 @if (!string.IsNullOrEmpty(searchTerm))
173 {
174 if (Model.TotalProductsCount > 0)
175 {
176 <li class="dropdown-item text-center p-2 text-wrap" id="ViewAllProductResults" onclick="swift.Typeahead.selectSuggestion(this);">
177 <span class="text-break text-decoration-underline">@Translate("View all") @Model.TotalProductsCount @Translate("products for")<mark> "<span class="js-suggestion">@searchTerm</span>"</mark></span>
178 </li>
179 }
180 else
181 {
182 <li class="dropdown-item text-center text-wrap @itemPadding" id="NoProductResults">
183 @Translate("We have no product results for")<mark> "<span class="js-suggestion">@searchTerm</span>"</mark>
184 </li>
185 }
186 }
187