Copied to clipboard

Flag this post as spam?

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


  • Gurjot Singh 5 posts 35 karma points
    Sep 23, 2014 @ 05:23
    Gurjot Singh
    0

    Adding divs in a @foreach based on a counter

    Hi,

    I am new to razor scripting with Umbraco and have been attempting to build a landing page with rows of 2.  I need to generate dynamic sections based on a counter.  The counter is based on the number of nodes of a specific parent. Based on the logic from what I can tell the div is getting closed when opened when out of the foreach loop.  

    getting the error below./

    Server Error in '/' Application.

    Parser Error

    Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 

    Parser Error Message: Encountered end tag "div" with no matching start tag.  Are your start/end tags properly balanced?


    Source Error: 

    Line 25:              @if(@count == 2);
    Line 26:              {
    Line 27:                @:</div>
    Line 28:                @: <div class="features-row">
    Line 29:                 count = 0;


    Source File: /Views/Partials/LearnLanding.cshtml    Line: 27 

    @inherits UmbracoTemplatePage
    @{
        int count = 0;
        
        var root = CurrentPage.AncestorsOrSelf(1).First();
        var nodes = root.Descendants("LearnPage");

    <section id="main" class="container">
        <section class="box learn features">
            <div class="features-row">
            @foreach (var node in nodes)
            {
                 @if(@count == 2);
                 {
                   @:</div>
                   @: <div class="features-row">
                    count = 0;
                 }
                 <section>
       <span class="icon major @node.iconType accent1"></span>
       <h3>@node.subTitle</h3>
       <p>@node.BodyText</p>
                    <a href="@node.Url" class="button alt">Learn More<a/>
       </section>
                count = count + 1;
            }
        </div>
            
       </section>
    </section>
    }

     

  • Nathan Woulfe 447 posts 1665 karma points MVP 5x hq c-trib
    Sep 23, 2014 @ 06:34
    Nathan Woulfe
    0

    You're wanting to build rows that each have two items in them, where each item is a descendant page of the root, correct?

    Could you change your approach to remove the need for multiple rows and instead create one outer row then style each inner section to float left with a 50% width? That should let you sit two side-by-side without having to worry about a counter and opening/closing new rows. If the row has margin/padding on top/bottom you could move that to the inner sections to maintain the spacing between.

    @inheritsUmbracoTemplatePage
    @{
       
    int count =0;
       
       
    var root =CurrentPage.AncestorsOrSelf(1).First();
       
    var nodes = root.Descendants("LearnPage");

       
    <div class="features-row">
           
    @foreach(var node in nodes)
           
    {          
                 
    <section class="float-left width-50-percent">
                    <span class="icon major @node.iconType accent1"></span>
                  <h3>@node.subTitle</h3>
                  <p>@node.BodyText</p>
                   
    <a href="@node.Url"class="button alt">LearnMore<a/>
             </section>

            }
    </div> 
  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 23, 2014 @ 06:56
    Sebastiaan Janssen
    101

    I'm currently not at a computer but you should investigate the InGroupsOf method so that you don't have all this super messy code.

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Sep 23, 2014 @ 09:13
    Steve Morgan
    0

    Sometimes Razor reports the wrong error. 

    This is syntax - try changing the line below - you've got an ampersand on your variable which you don't need as the @if has put it in Razor "mode" - and you've got a semi colon after the if - not valid. 

     @if(@count==2);

    To:

     @if(count==2)
Please Sign in or register to post replies

Write your reply to:

Draft