0
votes

I am using a partial view to list the top 5 children of a specific node.

This works, but only if I put a div before the

foreach

eg

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
<div class="title">Test</div>
<ul>
@{

var ow = @owCore.Initialise(1085); 
<div> </div>
var node = Umbraco.Content(1105);


foreach (var item in node
            .Children.Where("Visible")
            .OrderBy("Id descending")
         .Take(5)
        )
{

    <li><a href="@item.Url">@item.pageTitle</a></li>
}

}   
</ul>

produces the expected unsorted list.

However, if I remove the empty div

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage

Test
<ul>
@{

    var ow = @owCore.Initialise(1085); 

    var node = Umbraco.Content(1105);


    foreach (var item in node
                .Children.Where("Visible")
                .OrderBy("Id descending")
             .Take(5)
            )
    {

        <li><a href="@item.Url">@item.pageTitle</a></li>
    }

}   

The error I get is

Compiler Error Message: CS1513: } expected

Source Error:

Line 113: } Line 114: } Line 115:}

Clear looks like too few closing '}'

Presumably the div forces the closing }?

I have checked owCore (it's a library of functions I am building in App_Code : however, I have stripped this back and it's now doing nothing just to make sure there are matched curly brackets:

@using Umbraco
@using Umbraco.Core.Models
@using Umbraco.Web

@functions{



    public static int Initialise(int siteDocID){

            return 0; 
    }

}   

However, if I remove the @owCore code from the partial view

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage

Test
<ul>
@{



    var node = Umbraco.Content(1105);


    foreach (var item in node
                .Children.Where("Visible")
                .OrderBy("Id descending")
             .Take(5)
            )
    {

        <li><a href="@item.Url">@item.pageTitle</a></li>
    }

    }   
</ul>

All is ok again.

Does that mean it's definitely an issue with the owCore or simply something else tripping the issue with mismatched {}

I have checked the template calling this partial view and can't find a problem.

This doesn't make sense. Can anyone explain?

Thanks!

1
Not really an explanation, but I find that wrapping my variables in their own {} section and starting a new one with @foreach works most of the time... - Jannik Anker
Or maybe you shouldn't use @ in front af owCore? - Jannik Anker

1 Answers

0
votes

This is actually more of a razor question.

You start your code block with @{ and by doing that you don't need the @ in front of owCore. Removing it will make it render even without the <div> as the razor parser is no longer confused by the @.