How do i retrieve the TopPageID in RazorTemplates?
I am able to get the PageID like this:
@Pageview.Page.ID
BR
Finn Frost
How do i retrieve the TopPageID in RazorTemplates?
I am able to get the PageID like this:
@Pageview.Page.ID
BR
Finn Frost
I need it because i want to add some logic, to set a specific class attribute value, based on which main-menu(top-level-page) is selected.
Here is my template(for a news list), but the line where i try to get the top page id causes an error (incorrect way to get it!):
@{
string topPageCssClass = "varme";//string.Empty;
int pageId = 0;
int.TryParse(GetValue("Global:Page.Top.ID").ToString(), out pageId);
if(pageId == 0){ // This is the top page - get ID from page.
pageId = Pageview.Page.ID;
}
switch(pageId){
case 2:
topPageCssClass = "affald";
break;
case 3:
topPageCssClass = "vand";
break;
case 4:
topPageCssClass = "spildevand";
break;
case 5:
topPageCssClass = "varme";
break;
case 6:
topPageCssClass = "kunde";
break;
default:
break;
}
}
<script type="text/javascript">
console.log("Id: @pageId");
</script>
<div class="didYouKnowWrapper @topPageCssClass">
<div class="header @topPageCssClass"><h4>@GetValue("ParagraphHeader")</h4></div>
<div class="didYouKnowContent">
@GetValue("ParagraphModule")
</div>
</div>
I had to make the template work today, so i managed to convert the template to html, and achieve the same functionality.
The (Html)template below, and the (Razor)template in my previous comment, does excatly the same, however, i believe the Razor template is more readable, maintainable and of course less code!.
<!--@If Defined(DwTopPageID)-->
<!--@If(DwTopPageID=2)-->
<div class="didYouKnowWrapper afflad">
<div class="header affald"><h4><!--@ParagraphHeader--></h4></div>
<!--@EndIf-->
<!--@If(DwTopPageID=3)-->
<div class="didYouKnowWrapper vand">
<div class="header vand"><h4><!--@ParagraphHeader--></h4></div>
<!--@EndIf-->
<!--@If(DwTopPageID=4)-->
<div class="didYouKnowWrapper spildevand">
<div class="header spildevand"><h4><!--@ParagraphHeader--></h4></div>
<!--@EndIf-->
<!--@If(DwTopPageID=5)-->
<div class="didYouKnowWrapper varme">
<div class="header varme"><h4><!--@ParagraphHeader--></h4></div>
<!--@EndIf-->
<!--@If(DwTopPageID=6)-->
<div class="didYouKnowWrapper kunde">
<div class="header kunde"><h4><!--@ParagraphHeader--></h4></div>
<!--@EndIf-->
<!--@EndIf(DwTopPageID)-->
<!--@If Not Defined(DwTopPageID)-->
<!--@If(DwPageID=2)-->
<div class="didYouKnowWrapper afflad">
<div class="header affald"><h4><!--@ParagraphHeader--></h4></div>
<!--@EndIf-->
<!--@If(DwPageID=3)-->
<div class="didYouKnowWrapper vand">
<div class="header vand"><h4><!--@ParagraphHeader--></h4></div>
<!--@EndIf-->
<!--@If(DwPageID=4)-->
<div class="didYouKnowWrapper spildevand">
<div class="header spildevand"><h4><!--@ParagraphHeader--></h4></div>
<!--@EndIf-->
<!--@If(DwPageID=5)-->
<div class="didYouKnowWrapper varme">
<div class="header varme"><h4><!--@ParagraphHeader--></h4></div>
<!--@EndIf-->
<!--@If(DwPageID=6)-->
<div class="didYouKnowWrapper kunde">
<div class="header kunde"><h4><!--@ParagraphHeader--></h4></div>
<!--@EndIf-->
<!--@EndIf(DwTopPageID)-->
<div class="didYouKnowContent">
<!--@ParagraphModule-->
</div>
</div>
Although, it would still be nice to know how to get the TopPageID in Razor templates.
Best regards
Finn Frost
This is ugly, but it works :)
@using Dynamicweb;
@using Dynamicweb.Frontend;
@using Dynamicweb.Rendering;
@using System.Collections;
@model RazorTemplateModel<Template>
@functions {
int GetTopPageID() {
var topPageId = 0;
var pagePath = Pageview.Page.get_Value("pagepath") as ArrayList;
if (pagePath != null && pagePath.Count > 0)
{
var topPage = Pageview.PageCollection[pagePath[0]] as Page;
topPageId = Base.ChkInteger(topPage.get_Value("PageID"));
}
return topPageId;
}
}
@{
@ViewBag.TopPageID = GetTopPageID();
}
<div>@ViewBag.TopPageID</div>
You must be logged in to post in the forum