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 8992251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:54:38+00:00 2026-06-15T22:54:38+00:00

I am using umbraco 4. I want to loop through nodes and store its

  • 0

I am using umbraco 4. I want to loop through nodes and store its urlname in an array. So that array looks like … [‘aaa-node’,’bbb-node’,’ccc-node’]. But following code is not working…

<script type="text/javascript">
@{
var arr = new Array[10];
var j=0;

foreach (var node in Model.Children.Where("Visible"))
{
  arr[j]=@node.UrlName;
  j++;
}
}
</script>

But this gives Error loading Razor Script getnodes.cshtml
What is the mistake in code?

  • 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-15T22:54:40+00:00Added an answer on June 15, 2026 at 10:54 pm

    I’m guessing that you’re getting an index out of bounds exception since there could be more than 10 nodes.

    I find it easier to debug razor code by wrapping it in a try catch and then outputting the error. Note that this really only works for runtime errors.

    @try
    {
        // Initialize list:
        var list = new List<string>();
    
        // Add each urlname to the list:
        foreach (var node in Model.Children.Where("Visible"))
        {
            list.Add(node.UrlName);
        }
    
        // Serialize and output javascript:
        <script type="text/javascript">
            var arr = @Html.Raw(Json.Encode(list));
        </script>
    }
    catch (Exception ex)
    {
        @ex.ToString()
    }
    

    It’s a hard thing to wrap one’s mind around, I know. Razor code is evaluated server side and javascript is evaluated client side. So that means that there’s no real way for the two of them to work together.

    What we’re doing with the line of code between the script tags is writing out the javascript that will be run by the browser after it downloads the rendered page. As far as razor is concerned, it’s just writing out a string in that spot. Even though parts of the languages look alike, razor doesn’t know anything about javascript. Json.Encode (razor) serializes your list object into json. Html.Raw (razor) makes sure the output is not encoded. (Remove that and view the source of the page in the browser to get an idea as to why that’s necessary.)

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

Sidebar

Related Questions

I have an Umbraco website and i now started using the Urlrewriting.net that comes
I'm using qTip2 and with my Umbraco installation. I want my images inserted into
I'm new to Umbraco. I am using Umbraco 5 RC2. I have noticed that
I'm trying to create a vertical multilevel nav for Umbraco using XSLT, that will
I'm using Umbraco 4.7.1 and have created a custom DocumentType called Partner. I want
I'm having trouble using the umbraco default datatype related links. My problem is that
I have a razor script in Umbraco that is quite complex and I want
I using umbraco to create a multi territory website so I have nodes set
So I have a bunch of forms that I made using Umbraco contour and
I'm creating some xslt's using Umbraco 4.5.2 and I want to add de set:distinct

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.