I have following razor script. script file is saving successfully but error for rendering on page. Below is the code where RelatedBooks is doc type and prayerHeading is field of that doctype. So what's the mistake?
@* All CSS ID and Classes that have been applied in this script have been referenced in the GnanVidhi Page Template. *@ @using umbraco.BusinessLogic; @using dadabhagwan.org; @inherits umbraco.MacroEngines.DynamicNodeContext @{ var node = @Model.Node; if(@node.NodeTypeAlias == "RelatedBooks") { if(@node.prayerHeading == "Try yourself to experience guaranteed happiness - ") { <img alt="" class="left" src="/images/expand/try.png" align="absmiddle" /> } else if(@node.prayerHeading == "Keys to remain in Brahmacharya - ") { <img alt="" class="left" src="/images/expand/key.png" align="absmiddle" /> } else if(@node.prayerHeading == "Result of attaining Gnan Vidhi... - ") { <img alt="" class="left" src="/images/expand/solution.png" align="absmiddle" /> } else { <img alt="" class="left" src="/images/expand/try.png" align="absmiddle" /> } } }
As Jonas says, you already have the node with Model. So there is no need for var node = @Model.Node (and by the way, there is no need for @ either when you are already in a code block) just use:
if(Model.NodeTypeAlias == "RelatedBooks")
To ease your code you could use a switch instead of the if/else combinations:
switch((String)Model.prayerHeading){ case "Try yourself to experience guaranteed happiness - ": <img alt="" class="left" src="/images/expand/try.png" align="absmiddle" /> break; case "Keys to remain in Brahmacharya - ": <img alt="" class="left" src="/images/expand/key.png" align="absmiddle" /> break; default:<img alt="" class="left" src="/images/expand/try.png" align="absmiddle" /> break; }}
error rendering razor
I have following razor script. script file is saving successfully but error for rendering on page. Below is the code where RelatedBooks is doc type and prayerHeading is field of that doctype. So what's the mistake?
@*
All CSS ID and Classes that have been applied in this script have been referenced in the GnanVidhi Page Template.
*@
@using umbraco.BusinessLogic;
@using dadabhagwan.org;
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var node = @Model.Node;
if(@node.NodeTypeAlias == "RelatedBooks")
{
if(@node.prayerHeading == "Try yourself to experience guaranteed happiness - ")
{
<img alt="" class="left" src="/images/expand/try.png" align="absmiddle" />
}
else if(@node.prayerHeading == "Keys to remain in Brahmacharya - ")
{
<img alt="" class="left" src="/images/expand/key.png" align="absmiddle" />
}
else if(@node.prayerHeading == "Result of attaining Gnan Vidhi... - ")
{
<img alt="" class="left" src="/images/expand/solution.png" align="absmiddle" />
}
else
{
<img alt="" class="left" src="/images/expand/try.png" align="absmiddle" />
}
}
}
What is the error when rendering the page?
just that razor cannot be rendered.. .thats it..
I think i have problem with line ..... var node = @Model.Node; ... how can I access current node?
Hi,
actually you have it already with Model. So you should be able to use for example Model.PrayerHeading
As Jonas says, you already have the node with Model. So there is no need for var node = @Model.Node (and by the way, there is no need for @ either when you are already in a code block) just use:
if(Model.NodeTypeAlias == "RelatedBooks")
To ease your code you could use a switch instead of the if/else combinations:
switch((String)Model.prayerHeading){ case "Try yourself to experience guaranteed happiness - ": <img alt="" class="left" src="/images/expand/try.png" align="absmiddle" /> break; case "Keys to remain in Brahmacharya - ": <img alt="" class="left" src="/images/expand/key.png" align="absmiddle" /> break; default:<img alt="" class="left" src="/images/expand/try.png" align="absmiddle" /> break; }}thank you for guiding.. I am new to razor scripting...
I am new to Razor too :)
is working on a reply...