Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9271275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:34:51+00:00 2026-06-18T15:34:51+00:00

I am trying to do an if statement in razor for an umbraco website.

  • 0

I am trying to do an if statement in razor for an umbraco website. I need to get the page and all of its children by the Name property to be able to add a stylesheet to that page and any children in the tree

I can get the parent no problem but am having difficulties with the descendants as i am not sure where it would go?

So far I have:

@{
    var selectedNode = @Model.Name();  
    if(@selectedNode == "blue")
    {
        do this
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:34:53+00:00Added an answer on June 18, 2026 at 3:34 pm

    Looks like you’re a bit new to razor. This answer has a list of some great resources available to help you start learning about Umbraco’s razor syntax.

    In response to your question, I’m not sure if I understand exactly what you’re trying to accomplish. From what I can see, you want to add a style sheet to a page based on it’s name or the name of one of its ancestors. I’d suggest that it may not be the best option to key off of the name; a more stable solution would be to use the node’s id. But, here’s a code sample that should hopefully help:

    @{ 
        var nodes = Model.AncestorsOrSelf().Where("Name == @0", "Blue");
    
        if (nodes.Count() > 0)
        { 
            <link type="text/css" rel="Stylesheet" href="/css/style.css" />
        }
    }
    

    The above snippet gets the node’s ancestors (including itself) and filters them by the name of the node (“Blue”). Then it checks if any nodes are in the collection. If there are, then either the current node is “Blue” or it has an ancestor named “Blue” and therefore the style is applied.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add an if statement to my function, but it's not working.
Been trying to get this if-else statement to work, but every time I refresh
Trying to get the try/except statement working but having problems. This code will take
I'm trying to have an if statement inside a class property of a div
how to get tds which are visible. I am trying this statement but this
I was trying to use the following statement: @Html.Test<ISQL>().Nand() However, Razor is choking at
When trying to execute a statement on Oracle RDBMS I always get OCI_INVALID_HANDLE as
Im trying to get a simple SQL statement in my code, and get a
am trying to get an if else if statement to operate but am getting
I am trying to create a SELECT statement to obtain the sum of all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.