Error executing template "Designs/institution-skabelon/Paragraph/aaben_skole_products.cshtml"
System.NullReferenceException: Object reference not set to an instance of an object.
   at CompiledRazorTemplates.Dynamic.RazorEngine_fc5dda9f9095414f8a0f7ba69e049f01.Execute() in E:\dynamicweb.net\Solutions\rksk.net.dynamicweb-cms.com\Files\Templates\Designs\institution-skabelon\Paragraph\aaben_skole_products.cshtml:line 368
   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.b__0(TextWriter writer)
   at RazorEngine.Templating.RazorEngineServiceExtensions.WithWriter(Action`1 withWriter)
   at Dynamicweb.Rendering.Template.RenderRazorTemplate()

1 @inherits RazorTemplateBase<RazorTemplateModel<Template>> 2 @using System; 3 @using System.Web; 4 @using Dynamicweb; 5 @using Dynamicweb.Data; 6 @using Dynamicweb.Rendering; 7 @using Dynamicweb.Content.Items; 8 @using System.Data; 9 @using System.Collections; 10 @using System.Text.RegularExpressions 11 12 @inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>> 13 14 @using System.Web 15 @using System.Linq 16 17 @functions { 18 19 string GetImageUrl(string url, int width = 200, int height = 200) 20 { 21 if (string.IsNullOrWhiteSpace(url)) 22 { 23 url = string.Format("http://placehold.it/{0}x{1}", width, height); 24 } 25 if (!url.StartsWith("http://")) 26 { 27 url = string.Format("/Admin/Public/GetImage.ashx?Image={0}&width={1}&height={2}", url, width, height); 28 } 29 return url; 30 } 31 32 string GetAbsoluteUrl(string url) 33 { 34 var request = HttpContext.Current.Request; 35 return request.Url.Scheme + "://" + request.Url.Host + url; 36 } 37 38 string GetQueryParameter(string name) 39 { 40 return HttpContext.Current.Request[name]; 41 } 42 43 string BuildUrl(dynamic parameters, bool clear = false) 44 { 45 return BuildUrl(HttpContext.Current.Request.Url.PathAndQuery, parameters, clear); 46 } 47 48 string BuildUrl(string url, dynamic parameters, bool clear = false) 49 { 50 var dict = new Dictionary<string, object>(); 51 if (parameters != null) 52 { 53 var type = parameters.GetType(); 54 var properties = type.GetProperties(); 55 foreach (var property in properties) 56 { 57 dict.Add(property.Name, property.GetValue(parameters)); 58 } 59 } 60 return BuildUrl(url, dict, clear); 61 } 62 63 string BuildUrl(string url, Dictionary<string, object> parameters, bool clear = false) 64 { 65 var query = HttpUtility.ParseQueryString(""); 66 var index = url.IndexOf('?'); 67 if (index > -1) 68 { 69 if (!clear) 70 { 71 query = HttpUtility.ParseQueryString(url.Substring(index)); 72 } 73 url = url.Substring(0, index); 74 } 75 76 if (parameters != null) 77 { 78 foreach (var e in parameters) 79 { 80 query.Remove(e.Key); 81 query.Add(e.Key, e.Value == null ? "" : e.Value.ToString()); 82 } 83 } 84 85 if (query.Count > 0) 86 { 87 query.Remove("debug"); 88 query.Remove("dbstat"); 89 query.Remove("parse"); 90 query.Remove("purge"); 91 92 return string.Format("{0}?{1}", url, query); 93 } 94 else 95 { 96 return url; 97 } 98 } 99 100 public static string RemoveQueryStringByKey(string url, string key) 101 { 102 var uri = new Uri(url); 103 104 // this gets all the query string key value pairs as a collection 105 var newQueryString = HttpUtility.ParseQueryString(uri.Query); 106 107 // this removes the key if exists 108 newQueryString.Remove(key); 109 110 // this gets the page path from root without QueryString 111 string pagePathWithoutQueryString = uri.GetLeftPart(UriPartial.Path); 112 113 return newQueryString.Count > 0 114 ? String.Format("{0}?{1}", pagePathWithoutQueryString, newQueryString) 115 : pagePathWithoutQueryString; 116 } 117 } 118 119 @using System.Text.RegularExpressions; 120 121 @functions { 122 string GetExcerpt(string text, int length, string dots = " …") 123 { 124 var excerpt = Regex.Replace(System.Net.WebUtility.HtmlDecode(text), "<[^>]*>", ""); 125 if (excerpt.Length > length) 126 { 127 excerpt = excerpt.Remove(length); 128 excerpt = Regex.Replace(excerpt, @"\s[^\s]+$", ""); 129 excerpt += dots; 130 } 131 return excerpt; 132 } 133 } 134 135 @functions { 136 string StripTags(string content) 137 { 138 return Regex.Replace(content, "<[^>]*>", ""); 139 } 140 141 string XmlEncode(object value) 142 { 143 return value != null ? XmlEncode(value.ToString()) : null; 144 } 145 146 string XmlEncode(string value) 147 { 148 return System.Net.WebUtility.HtmlEncode(System.Net.WebUtility.HtmlDecode(value)); 149 } 150 151 string XmlElement(string elementName, object content) 152 { 153 var s = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}", content); 154 if (string.IsNullOrEmpty(s)) 155 { 156 return "<" + elementName + "/>"; 157 } 158 else 159 { 160 // Prevent Html Agility Pack from messing up the markup 161 return "<" + elementName + ">" + s + "<" + "/" + elementName + ">"; 162 } 163 } 164 } 165 166 @using Dynamicweb.Content.Items; 167 168 @functions { 169 IEnumerable<Item> GetItemsByIds(string itemType, string idList) 170 { 171 var ids = idList.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList<string>(); 172 return GetItemsByIds(itemType, ids); 173 } 174 175 IEnumerable<Item> GetItemsByIds(string itemType, List<string> ids) 176 { 177 try 178 { 179 var repo = new Dynamicweb.Content.Items.Queries.Repository(itemType); 180 var items = repo.SelectByIds(ids).OrderBy(i => ids.IndexOf(i.Id)); 181 return items; 182 } 183 catch { } 184 return null; 185 } 186 } 187 @helper DUMP(object value, string heading = null) 188 { 189 // https://github.com/dynamicweb/razor/wiki/Using-Json.Net-to-inspect-values 190 <pre class="dw-dump">@if (heading != null) {<text>@heading</text> 191 } 192 @Newtonsoft.Json.JsonConvert.SerializeObject(value, new Newtonsoft.Json.JsonSerializerSettings { 193 ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore, 194 MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore, 195 Error = (serializer,err) => err.ErrorContext.Handled = true, 196 Formatting = Newtonsoft.Json.Formatting.Indented 197 }) 198 </pre> 199 } 200 @helper Paging(int all_results_count, int result_per_page, string goto_url = "") 201 { 202 //check if goto URL is set, if not, use current URL 203 if (String.IsNullOrEmpty(goto_url)) 204 { 205 goto_url = GetAbsoluteUrl(HttpContext.Current.Request.RawUrl); 206 goto_url = RemoveQueryStringByKey(goto_url, "page"); 207 } 208 209 //set the query base, so we can easily manipulate with URL for paging after 210 string querybase = ""; 211 if (goto_url.Contains("?")) 212 { 213 querybase = goto_url + "&page="; 214 } 215 else 216 { 217 querybase = goto_url + "?page="; 218 } 219 220 //What page are we on? 221 int currentPage = 1; 222 if (!String.IsNullOrEmpty(HttpContext.Current.Request["page"])) 223 { 224 currentPage = Convert.ToInt32(HttpContext.Current.Request["page"]); 225 } 226 227 //How many pages do we have? 228 double numberOfPages_tmp = (double)all_results_count / (double)result_per_page; 229 double numberOfPages = Math.Ceiling(numberOfPages_tmp); 230 231 //Determine start and end and how many to display 232 int vis_antal = 4; 233 double start = currentPage - vis_antal; 234 double slut = currentPage + vis_antal; 235 236 if ((currentPage - start) < vis_antal + 1) 237 { 238 slut = slut + (vis_antal - (currentPage - start)) + 1; 239 } 240 241 if ((slut - currentPage) < vis_antal) 242 { 243 start = start - (vis_antal - (slut - currentPage)); 244 } 245 246 if (start < 1) 247 { 248 start = 1; 249 } 250 251 if (slut > numberOfPages) 252 { 253 slut = numberOfPages; 254 } 255 256 if (currentPage > 5 && numberOfPages < 8) 257 { 258 start = 2; 259 } 260 261 int prevPage = currentPage - 1; 262 int nextPage = currentPage + 1; 263 264 //if we have any pages to display, do it 265 string query = ""; 266 if (numberOfPages > 1) 267 { 268 269 <ul class="nav nav--paging"> 270 271 @if (currentPage > 1) 272 { 273 query = querybase + prevPage; 274 <li class="nav--paging-item"> 275 <a href='@query' class="icon">&#xf100;</a> 276 </li> 277 } 278 279 @if (currentPage > vis_antal + 1) 280 { 281 query = querybase + "1"; 282 <li class="nav--paging-item hidden-xs"><a href='@query'>1</a></li> 283 } 284 285 @if (currentPage > vis_antal + 2) 286 { 287 if (start == 3) 288 { 289 query = querybase + "2"; 290 <li class="nav--paging-item hidden-xs"><a href='javascript:void(0);'>2</a></li> 291 } 292 else if (start == 2) 293 { 294 295 } 296 else 297 { 298 <li class="nav--paging-item hidden-xs">...</li> 299 } 300 } 301 302 @for (double page_i = start; page_i <= slut; page_i++) 303 { 304 if (page_i == currentPage) 305 { 306 <li class="nav--paging-item nav--search-paging-item--active hidden-xs">@page_i</li> 307 } 308 else 309 { 310 query = querybase + page_i; 311 <li class="nav--paging-item hidden-xs"><a href='@query' title="">@page_i</a></li> 312 } 313 } 314 315 @if (slut + 1 < numberOfPages) 316 { 317 if (slut == numberOfPages - 2) 318 { 319 slut++; 320 query = querybase + slut; 321 <li class="nav--paging-item hidden-xs"><a href='@query'>@slut</a></li> 322 } 323 else 324 { 325 <li class="nav--paging-item hidden-xs">...</li> 326 } 327 } 328 329 @if (slut < numberOfPages) 330 { 331 query = querybase + numberOfPages; 332 <li class="nav--paging-item hidden-xs"><a href='@query'>@numberOfPages</a></li> 333 } 334 335 @if (currentPage < numberOfPages) 336 { 337 query = querybase + nextPage; 338 <li class="nav--paging-item"> 339 <a href="@query" class="icon">&#xf101;</a> 340 </li> 341 } 342 343 </ul> 344 345 } 346 347 } 348 349 <div class="aabenskole--product-container"> 350 @{ 351 352 //get item info for page 353 Item item_page_info = Dynamicweb.Content.Items.ItemManager.Storage.GetById("Template___ben_Skole_Listeside", Pageview.Item.Id); 354 355 //build query where for paging 356 string queryWhereExtend = ""; 357 358 //Query possibilities from search 359 string query_term = GetQueryParameter("term"); 360 if (!String.IsNullOrEmpty(query_term)) 361 { 362 query_term = query_term.ToLower(); 363 queryWhereExtend = queryWhereExtend + " AND (a.Tekst LIKE '%'+@SQL_TERM+'%' OR a.Overskrift LIKE '%'+@SQL_TERM+'%')"; 364 } 365 366 string query_topic = GetQueryParameter("Emne"); 367 string current_topic = ""; 368 if (item_page_info["Emne"] != null && !string.IsNullOrEmpty(item_page_info["Emne"].ToString())) 369 { 370 query_topic = item_page_info["Emne"].ToString(); 371 } 372 if (!String.IsNullOrEmpty(query_topic)) 373 { 374 queryWhereExtend = queryWhereExtend + " AND a.Emne LIKE '%'+@SQL_TOPIC+'%'"; 375 } 376 377 string query_subject = GetQueryParameter("Fag"); 378 if (!String.IsNullOrEmpty(query_subject)) 379 { 380 queryWhereExtend = queryWhereExtend + " AND a.Fag LIKE '%'+@SQL_SUBJECT+'%'"; 381 } 382 383 string query_ages = GetQueryParameter("Alderstrin"); 384 if (!String.IsNullOrEmpty(query_ages)) 385 { 386 queryWhereExtend = queryWhereExtend + " AND a.Alderstrin LIKE '%'+@SQL_AGES+'%'"; 387 } 388 389 string query_period = GetQueryParameter("Periode"); 390 if (!String.IsNullOrEmpty(query_period)) 391 { 392 queryWhereExtend = queryWhereExtend + " AND a.Periode LIKE '%'+@SQL_PERIOD+'%'"; 393 } 394 395 //What page are we on? 396 int current_page = 1; 397 if (!String.IsNullOrEmpty(HttpContext.Current.Request["page"])) 398 { 399 current_page = Convert.ToInt32(HttpContext.Current.Request["page"]); 400 } 401 402 //Paging vars 403 int sql_offset_start = ((current_page * 8) - 8) + 1; 404 int sql_offset_end = current_page * 8; 405 406 int all_results_count = 0; 407 using (IDbConnection connection = Database.CreateConnection()) 408 { 409 410 //Count totalpages for paging 411 IDbCommand command_count = connection.CreateCommand(); 412 command_count.CommandText = @"SELECT COUNT(a.Id) FROM ItemType_Itemliste___ben_Skole_Produkt AS a 413 INNER JOIN ItemListRelation AS b ON a.Id = b.ItemListRelationItemId 414 WHERE b.ItemListRelationItemListId=1" + queryWhereExtend; 415 Database.AddStringParam(command_count, "@SQL_TERM", query_term); 416 Database.AddStringParam(command_count, "@SQL_TOPIC", query_topic); 417 Database.AddStringParam(command_count, "@SQL_SUBJECT", query_subject); 418 Database.AddStringParam(command_count, "@SQL_AGES", query_ages); 419 Database.AddStringParam(command_count, "@SQL_PERIOD", query_period); 420 421 //Execute query and get count 422 all_results_count = Convert.ToInt32(command_count.ExecuteScalar()); 423 424 //Execute query and page it 425 IDbCommand command = connection.CreateCommand(); 426 command.CommandText = @";WITH t AS( 427 SELECT a.*, 428 ROW_NUMBER() OVER(ORDER BY a.Id) AS 'RowNumber' 429 FROM ItemType_Itemliste___ben_Skole_Produkt As a 430 INNER JOIN ItemListRelation AS b ON a.Id = b.ItemListRelationItemId 431 WHERE b.ItemListRelationItemListId=1" + queryWhereExtend + ") SELECT * FROM t WHERE RowNumber BETWEEN @SQL_OFFSET_START AND @SQL_OFFSET_END;"; 432 433 Database.AddStringParam(command, "@SQL_TERM", query_term); 434 Database.AddStringParam(command, "@SQL_TOPIC", query_topic); 435 Database.AddStringParam(command, "@SQL_SUBJECT", query_subject); 436 Database.AddStringParam(command, "@SQL_AGES", query_ages); 437 Database.AddStringParam(command, "@SQL_PERIOD", query_period); 438 Database.AddInt32Param(command, "@SQL_OFFSET_START", sql_offset_start); 439 Database.AddInt32Param(command, "@SQL_OFFSET_END", sql_offset_end); 440 441 //Execute query 442 var reader = command.ExecuteReader(); 443 444 //transform to datatable 445 DataTable dt = new DataTable(); 446 dt.Load(reader); 447 448 <div class="row"> 449 <div class="col-md-6"> 450 <h1 class="aabenskole--product-headline">@GetGlobalValue("Global:Page.Name")</h1> 451 </div> 452 <div class="col-md-6"> 453 <div class="aabenskole--paging-listpagetop pull-right"> 454 @Paging(all_results_count, 8) 455 </div> 456 </div> 457 </div> 458 459 foreach (DataRow row in dt.Rows) 460 { 461 462 //dim vars 463 string headline = row["Overskrift"].ToString(); 464 string text = row["Tekst"].ToString(); 465 string subject = row["Fag"].ToString(); 466 string ages = row["Alderstrin"].ToString(); 467 string topic = row["Emne"].ToString(); 468 string period = row["Periode"].ToString(); 469 string prices = row["Priser"].ToString(); 470 471 string placement = row["Placering"].ToString(); 472 string lat = ""; 473 string lng = ""; 474 string street = row["Street"].ToString(); 475 string number = row["Number"].ToString(); 476 string ZIP = row["ZIP"].ToString(); 477 string city = row["City"].ToString(); 478 if (!String.IsNullOrEmpty(placement)) 479 { 480 dynamic placement_arr = Newtonsoft.Json.JsonConvert.DeserializeObject(placement); 481 lat = placement_arr.Lat; 482 lng = placement_arr.Lng; 483 484 } 485 486 string link = row["Link"].ToString(); 487 string image = row["Billede"].ToString(); 488 string video = row["Video"].ToString(); 489 490 //reize image 491 string resized_image = ""; 492 if (Pageview.Device == Dynamicweb.Frontend.Devices.DeviceType.Mobile) 493 { 494 resized_image = GetImageUrl(image, 730, 500); 495 } 496 else 497 { 498 resized_image = GetImageUrl(image, 730, 500); 499 500 } 501 502 503 504 <div class="row aabenskole--product"> 505 <div class="col-md-12"> 506 507 <div class="aabenskole--product-image" style="background-image: url(@resized_image)"> 508 @if (!String.IsNullOrEmpty(lat) && !String.IsNullOrEmpty(lng)) 509 { 510 <a class="aabenskole--product-map fancybox fancybox.iframe icon" href="http://www.rksk.dk/emner/google-maps?lat=@lat&lng=@lng&street=@street&number=@number&ZIP=@ZIP&city=@city&Purge=TRUE"> 511 &#xf041; 512 </a> 513 } 514 515 @if (!String.IsNullOrEmpty(video)) 516 { 517 518 <img class="youtube" id="@video" src="/Files/Images/play-circle.svg" style="height:100px;opacity: 0.7; 519 filter: alpha(opacity=40);position:absolute;top:35%;left:40%;cursor:pointer;" /> 520 521 } 522 </div> 523 524 <div class="aabenskole--product-content"> 525 526 <div class="aabenskole--product-border"> 527 <h2>@headline</h2> 528 529 <div class="aabenskole--product-text"> 530 @text 531 </div> 532 533 <div class="aabenskole--product-options"> 534 <table> 535 <tr> 536 <td class="aabenskole--product-options-title">Fag:</td> 537 <td> 538 @{ 539 string[] subjects_arr = subject.Split(','); 540 foreach (string word in subjects_arr) 541 { 542 <span class="aabenskole--product-options-selected">@HttpUtility.UrlDecode(word.Replace("Haandvaerk", "Håndværk"))</span> 543 } 544 } 545 </td> 546 </tr> 547 <tr> 548 <td class="aabenskole--product-options-title">Alder:</td> 549 <td> 550 @{ 551 string[] ages_arr = ages.Split(','); 552 foreach (string word in ages_arr) 553 { 554 <span class="aabenskole--product-options-selected">@word</span> 555 } 556 } 557 </td> 558 </tr> 559 <tr> 560 <td class="aabenskole--product-options-title">Emne:</td> 561 <td> 562 @{ 563 string[] topic_arr = topic.Split(','); 564 foreach (string word in topic_arr) 565 { 566 <span class="aabenskole--product-options-selected">@word.Replace("Miljoe", "Miljø")</span> 567 568 } 569 } 570 </td> 571 </tr> 572 <tr> 573 <td class="aabenskole--product-options-title">Periode:</td> 574 <td> 575 @{ 576 string[] period_arr = period.Split(','); 577 foreach (string word in period_arr) 578 { 579 <span class="aabenskole--product-options-selected">@word</span> 580 581 } 582 } 583 </td> 584 </tr> 585 </table> 586 </div> 587 </div> 588 589 <div class="aabenskole--product-price-container"> 590 <div class="aabenskole--product-price"> 591 <strong>Pris:</strong> @prices 592 </div> 593 <a href="@link" target="_blank" class="btn btn-primary aabenskole--product-btn">Se mere her</a> 594 </div> 595 596 </div> 597 598 </div> 599 </div> 600 } 601 } 602 603 <div class="aabenskole--paging-listpagebottom pull-right"> 604 @Paging(all_results_count, 8) 605 </div> 606 } 607 </div> 608 609 @if (all_results_count == 0) 610 { 611 <div class="h5"> - Ikke nogle l&aelig;ringstilbud i &oslash;jeblikket</div> 612 } 613 614