Copied to clipboard

Flag this post as spam?

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


  • hetaurhet 245 posts 267 karma points
    Mar 11, 2012 @ 07:33
    hetaurhet
    0

    razor nodes.count() problem

    hello..

    I have following code in razor which works fine....

    @using umbraco.BusinessLogic;
    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNode
    @inherits umbraco.MacroEngines.DynamicNodeContext
    <link href="../../scripts/fancybox/jquery.fancybox-1.3.4.css" type="text/css" rel="Stylesheet" />

    <script src="../../scripts/jquery-1.5.2.min.js" type="text/javascript"></script>
    <script src="../../scripts/fancybox/jquery.fancybox-1.3.4.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function () {

            /* This is basic - uses default settings */

            $("a#single_image").fancybox();
            $("a#single_download").fancybox();

            /* Using custom settings */

            $("a#inline").fancybox({
                'hideOnContentClick': true
            });

            /* Apply fancybox to multiple items */

            $("a.group").fancybox({
                'transitionIn': 'elastic',
                'transitionOut': 'elastic',
                'speedIn': 600,
                'speedOut': 200,
                'overlayShow': false
            });

        });
     
    </script>
    @{
      
     string comp = (Request.QueryString["comp"]);

     var node = Model.Children;
     var children = Model.Children;
      
     if(comp!=null)  
     {
      node=@Model.Children.Where("Name.ToString().ToLower() = @0", comp).FirstOrDefault();
     }
     else
     {
      children = Model.Children;
      node = children.First();
     
     }
      

     if(node != null)
     {
        foreach(var node1 in node.Children)
       {               
        var link = Library.MediaById(node1.competitionThumbImage);
        var link2 = Library.MediaById(node1.competitionImage);
                       
        <div class="thumb">
          <div class="new-tag">
            <a href="@link2.umbracoFile" id="single_image" title='@node1.Name'><img src="@link.umbracoFile" />
            @if(@node1.GetProperty("isFirst").Value.ToString() != null && @node1.isFirst)
            {
             <img src="/images/talent-corner/talent-tag-1.png" class="talent-tag" />
            }
            else if(@node1.GetProperty("isSecond").Value.ToString() != null && @node1.isSecond)
            {
             <img src="/images/talent-corner/talent-tag-2.png" class="talent-tag" />
            }
            else if(@node1.GetProperty("isThird").Value.ToString() != null && @node1.isThird)
            {
             <img src="/images/talent-corner/talent-tag-3.png" class="talent-tag" />
            }
            <h3>@node1.Name</h3></a>
          </div>
         </div>
        }
      }
    }

    But now in above code I want to add just one if statement.. that if node.Count()!=0 then only render above script else one line statement in <p></p> tag is to be shown... so now code looks like as below. But this is giving razor rendering error when no child nodes are present... what is the problem..?

    @using umbraco.BusinessLogic;
    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNode
    @inherits umbraco.MacroEngines.DynamicNodeContext
    <link href="../../scripts/fancybox/jquery.fancybox-1.3.4.css" type="text/css" rel="Stylesheet" />

    <script src="../../scripts/jquery-1.5.2.min.js" type="text/javascript"></script>
    <script src="../../scripts/fancybox/jquery.fancybox-1.3.4.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function () {

            /* This is basic - uses default settings */

            $("a#single_image").fancybox();
            $("a#single_download").fancybox();

            /* Using custom settings */

            $("a#inline").fancybox({
                'hideOnContentClick': true
            });

            /* Apply fancybox to multiple items */

            $("a.group").fancybox({
                'transitionIn': 'elastic',
                'transitionOut': 'elastic',
                'speedIn': 600,
                'speedOut': 200,
                'overlayShow': false
            });

        });
     
    </script>
    @{
      
     string comp = (Request.QueryString["comp"]);

     var node = Model.Children;
     var children = Model.Children;
    if(node.Count()!=0)
    {  
     if(comp!=null)  
     {
      node=@Model.Children.Where("Name.ToString().ToLower() = @0", comp).FirstOrDefault();
     }
     else
     {
      children = Model.Children;
      node = children.First();
     
     }
      

     if(node != null)
     {
        foreach(var node1 in node.Children)
       {               
        var link = Library.MediaById(node1.competitionThumbImage);
        var link2 = Library.MediaById(node1.competitionImage);
                       
        <div class="thumb">
          <div class="new-tag">
            <a href="@link2.umbracoFile" id="single_image" title='@node1.Name'><img src="@link.umbracoFile" />
            @if(@node1.GetProperty("isFirst").Value.ToString() != null && @node1.isFirst)
            {
             <img src="/images/talent-corner/talent-tag-1.png" class="talent-tag" />
            }
            else if(@node1.GetProperty("isSecond").Value.ToString() != null && @node1.isSecond)
            {
             <img src="/images/talent-corner/talent-tag-2.png" class="talent-tag" />
            }
            else if(@node1.GetProperty("isThird").Value.ToString() != null && @node1.isThird)
            {
             <img src="/images/talent-corner/talent-tag-3.png" class="talent-tag" />
            }
            <h3>@node1.Name</h3></a>
          </div>
         </div>
        }
      }
     }
    else
    {
     <p>No items for now</p>
    }
    }
  • Bas 3 posts 24 karma points
    Mar 15, 2012 @ 14:45
    Bas
    0

    Maybe because in the loop of more than 0 youre checking if it even exists?

    if(node != null)

    maybe try doing this above the check how many children it has?


Please Sign in or register to post replies

Write your reply to:

Draft