Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Meni 271 posts 507 karma points
    May 02, 2013 @ 02:04
    Meni
    0

    Razor Error bug

    Hello, 

    I'm using Umbraco 6.0.4

    I tried to use one if the code samples from the Umbraco website (which I downloaded from Umbraco website)  to a Macro Script (Developers - Scripting Files) but always got the following error:

    "cs(433): error CS1513: } expected"

    And this is the code I use - just copy-paste - didn't change nothing - I took it from here:

    http://umbraco.com/help-and-support/video-tutorials/umbraco-fundamentals/razor-recipes/news.aspx

    @{
        var pagesToList = @Model.Children.Where("Visible");
    
        //Check macro param is not empty, if so default to 6 otherwise use the Parameter value
        var itemsPerPage = String.IsNullOrEmpty(Parameter.noPerPage) ? 6 : int.Parse(Parameter.noPerPage);
    
        //Count number of items from pagesToList variable
        var numberOfItems = pagesToList.Count();
    
        //Set the currentPage to 1
        int currentPage = 1;
    
        /*
            If we cannot parse a number from requesting the querystring param 'page' then set to 1,
            otherwise set the querystring value to our currentPage variable
        */
        if (!int.TryParse(HttpContext.Current.Request.QueryString["Page"], out currentPage))
        {
            //No querystring found or not a number, so set currentPage to 1
            currentPage = 1;
        }
    
        //Subtract one from the currentPage variable (to use a zero based index, first page = 0)
        currentPage--;
    
        //Number of Pages (Maths time!)
        /*
            Divide the numberOfItems by itemsPerPage and then round the number up to 
            next whole number and add one to the result (eg: 5.1 = 6 + 1 = 7) 
        */
        var numberOfPages = Math.Ceiling((decimal)numberOfItems / (decimal)itemsPerPage);
    
    
        //Display the items in groups of 3 wrapped with a <div>
        <div id="gridText">
            @* 
                For each newsItem from our source (pagesToList)
                Where the page querystring = 2 /currentPage = 1...
                Skip over 6 records (currentPage * itemsPerPage) eg: 1*6 = 6
                Take 6 records, which grabs 7,8,9,10,11 & 12
            *@
            @foreach (var itemGroup in pagesToList.Skip(currentPage*itemsPerPage).Take(itemsPerPage) .InGroupsOf(3))
            {
               <div class="group">
                    @foreach (var newsItem in itemGroup)
                    {
                        var newsCopy = newsItem.BodyText;
                        newsCopy = Library.StripHtml(newsCopy);
                        newsCopy = Library.Truncate(newsCopy, 50, true);
    
                        <article class="item">
                            <h3><a href="@newsItem.Url">@newsItem.Name</a></h3>
                            <p>@newsCopy</p>
                        </article>           
                    }
                </div>
            }
        </div>
    
    
        <ul id="paging" class="group">
        @{
            /*
                pageQuerystring = 1 == 0 = currentPage
                pageQuerystring = 2 == 1 = currentPage
            */        
    
            //PREVIOUS Link
            <li>
                @if (currentPage > 0)
                {
                    <a href="?page=@(currentPage)">&laquo; Previous</a>
                }
                else
                {
                    <span class="pagingDisabled">&laquo; Previous</span>
                }
            </li>
    
    
            //Create a variable 'pages' that stores from 1 to the numberOfPages variable
            var pages = Enumerable.Range(1, (int)numberOfPages);
    
            //Loop through the numbers in the Pages variable
            foreach (var number in pages)
            {
                <li>
                    @if (number - 1 != currentPage)
                    {
                        <a href="?page=@number">@number</a>
                    }
                    else
                    {
                        <span>@number</span>
                    }
                </li>
            }
    
            //NEXT Link
            <li>
                @if (currentPage < pages.Count() - 1)
                {
                    <a href="?page=@(currentPage + 2)">Next &raquo;</a>
                }
                else
                {
                    <span class="pagingDisabled">Next &raquo;</span>
                }
            </li>
        }
        </ul>
    }
    

    Now, the line that cause the error is this:

     if (!int.TryParse(HttpContext.Current.Request.QueryString["Page"], out currentPage))
        {
            //No querystring found or not a number, so set currentPage to 1
            currentPage = 1;
        }
        

    The wired thing is that if for example I put it into <span> </span> there isn't error... 

    I can live without the "If" statement but the point is that I want to add also switch statment to this code (in order to handle each category) - but I can't do it with this error - did anyone had also this error?

    How do I solve it? I tried to add as many "}" as possible but it's not accept it - the error just keep show.

     

    Thanks

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    May 02, 2013 @ 09:11
    Dave Woestenborghs
    0

    Can you try it like this :

    @{
        var pagesToList = @Model.Children.Where("Visible");
    
        //Check macro param is not empty, if so default to 6 otherwise use the Parameter value
        var itemsPerPage = String.IsNullOrEmpty(Parameter.noPerPage) ? 6 : int.Parse(Parameter.noPerPage);
    
        //Count number of items from pagesToList variable
        var numberOfItems = pagesToList.Count();
    
        //Set the currentPage to 1
        int currentPage = 1;
    
        /*
            If we cannot parse a number from requesting the querystring param 'page' then set to 1,
            otherwise set the querystring value to our currentPage variable
        */
        if (!int.TryParse(HttpContext.Current.Request.QueryString["Page"], out currentPage))
        {
            //No querystring found or not a number, so set currentPage to 1
            currentPage = 1;
        }
    
        //Subtract one from the currentPage variable (to use a zero based index, first page = 0)
        currentPage--;
    
        //Number of Pages (Maths time!)
        /*
            Divide the numberOfItems by itemsPerPage and then round the number up to 
            next whole number and add one to the result (eg: 5.1 = 6 + 1 = 7) 
        */
        var numberOfPages = Math.Ceiling((decimal)numberOfItems / (decimal)itemsPerPage);
    } 
    
        //Display the items in groups of 3 wrapped with a <div>
        <div id="gridText">
            @* 
                For each newsItem from our source (pagesToList)
                Where the page querystring = 2 /currentPage = 1...
                Skip over 6 records (currentPage * itemsPerPage) eg: 1*6 = 6
                Take 6 records, which grabs 7,8,9,10,11 & 12
            *@
            @foreach (var itemGroup in pagesToList.Skip(currentPage*itemsPerPage).Take(itemsPerPage) .InGroupsOf(3))
            {
               <div class="group">
                    @foreach (var newsItem in itemGroup)
                    {
                        var newsCopy = newsItem.BodyText;
                        newsCopy = Library.StripHtml(newsCopy);
                        newsCopy = Library.Truncate(newsCopy, 50, true);
    
                        <article class="item">
                            <h3><a href="@newsItem.Url">@newsItem.Name</a></h3>
                            <p>@newsCopy</p>
                        </article>           
                    }
                </div>
            }
        </div>
    
    
        <ul id="paging" class="group">
        @{
            /*
                pageQuerystring = 1 == 0 = currentPage
                pageQuerystring = 2 == 1 = currentPage
            */        
    
            //PREVIOUS Link
            <li>
                @if (currentPage > 0)
                {
                    <a href="?page=@(currentPage)">&laquo; Previous</a>
                }
                else
                {
                    <span class="pagingDisabled">&laquo; Previous</span>
                }
            </li>
    
    
            //Create a variable 'pages' that stores from 1 to the numberOfPages variable
            var pages = Enumerable.Range(1, (int)numberOfPages);
    
            //Loop through the numbers in the Pages variable
            foreach (var number in pages)
            {
                <li>
                    @if (number - 1 != currentPage)
                    {
                        <a href="?page=@number">@number</a>
                    }
                    else
                    {
                        <span>@number</span>
                    }
                </li>
            }
    
            //NEXT Link
            <li>
                @if (currentPage < pages.Count() - 1)
                {
                    <a href="?page=@(currentPage + 2)">Next &raquo;</a>
                }
                else
                {
                    <span class="pagingDisabled">Next &raquo;</span>
                }
            </li>
        }
        </ul>
    
    

    I moved the last } to a place before the first <div>-tag

    Dave

  • Meni 271 posts 507 karma points
    May 03, 2013 @ 00:04
    Meni
    0

    Hi Dave,

    Thanks, but, yet I get the error message " error CS1513: } expected" ..... (even after copy-paste your code ...)

  • Meni 271 posts 507 karma points
    May 03, 2013 @ 00:05
    Meni
    0

    And of course if I delete the "if" statement it's doesn't give the error ...

  • Meni 271 posts 507 karma points
    May 03, 2013 @ 00:13
    Meni
    0

    And furthemore, even if I just use this code 



    @{
        var pagesToList = @Model.Children.Where("Visible");
    
        //Check macro param is not empty, if so default to 6 otherwise use the Parameter value
        var itemsPerPage = String.IsNullOrEmpty(Parameter.noPerPage) ? 6 : int.Parse(Parameter.noPerPage);
    
        //Count number of items from pagesToList variable
        var numberOfItems = pagesToList.Count();
    
        //Set the currentPage to 1
        int currentPage = 1;
    
        /*
            If we cannot parse a number from requesting the querystring param 'page' then set to 1,
            otherwise set the querystring value to our currentPage variable
        */
        if (!int.TryParse(HttpContext.Current.Request.QueryString["Page"], out currentPage))
        {
            //No querystring found or not a number, so set currentPage to 1
            currentPage = 1;
        }
    
        //Subtract one from the currentPage variable (to use a zero based index, first page = 0)
        currentPage--;
    
        //Number of Pages (Maths time!)
        /*
            Divide the numberOfItems by itemsPerPage and then round the number up to 
            next whole number and add one to the result (eg: 5.1 = 6 + 1 = 7) 
        */
        var numberOfPages = Math.Ceiling((decimal)numberOfItems / (decimal)itemsPerPage);
    
    }
     

    I still get the error. If I remove the if statement though - then there isn't error message ... 

    I think it's a bug in 6.0.4

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    May 03, 2013 @ 08:51
    Dave Woestenborghs
    0

    Looking at the code, you actualy don't need the if statement.

    The value of currentPage is already 1. So you don't need to set it when the TryParse is false.

    Also if i use this script in a 4.x installation it doesn't give any errors

  • Meni 271 posts 507 karma points
    May 03, 2013 @ 11:02
    Meni
    0

    But I need a switch statement because I want to use it as an archieve page and I need to get the category of the current page in order to choose the right node - anyway, the bottom line is that it's a bug.. - becasue it's not allowed to use if / switch statement after the var

     

    I know it's work on 4.x - but like I said the bug is on 6.x - which is annonying..  - and [I think ..] bugs should be fix ...

  • gary 385 posts 916 karma points
    May 03, 2013 @ 19:58
    gary
    0

    Hi Meni

    Not a bug but Razor V2, you have @ where it is now not needed, when you are in a codeblock ie @{}, a further @ is not needed, in fact it breaks the code..

    So,

    @{ var pagesToList = Model.Children.Where("Visible") etc. . . } should be sufficient.

    It is odd because the error says you are missing a }, you are! but it should be in front of the @Model, so if the error said you have an @ extra, then it may make more sense. It is saying you have not "closed" the codeblock before opening another one with the @Model.

    Hope it makes sense

    Gary

  • Meni 271 posts 507 karma points
    May 05, 2013 @ 02:37
    Meni
    0

    Like a magic Gary!

     

    Thank you!

Please Sign in or register to post replies

Write your reply to:

Draft