Imagine being paid to improve your favorite CMS!

It could be you! Umbraco HQ are hiring and are looking for both talents and senior developers. Wonderful workplace, friendly colleagues and competitive salary.

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
    Feb 27, 2012 @ 16:08
    hetaurhet
    0

    filter nodes

    I want to filter child nodes by node name... say I have string variable to which I will compare node name...

    so how can I do that in razor?

  • Rodion Novoselov 694 posts 858 karma points
    Feb 27, 2012 @ 16:32
    Rodion Novoselov
    0

    Hi. Try something like:

    var nodes = Model.Children.Where("Name = @0", name);

     

  • hetaurhet 245 posts 267 karma points
    Feb 28, 2012 @ 06:58
    hetaurhet
    0

    ok thanks I used as u said...

    now my code is like .... it shows error as Error loading MacroEngine script (file: CompetitionDropDown.cshtml) when page is run. no error on saving razor file.

    @using umbraco.BusinessLogic;
    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNode
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
    string compval="";

       <select id="ddlcompetition">
                @{
                int i=1;
                foreach (var item in @Model.Children.OrderBy("CreateDate desc"))
                { if(i==1)
                  {
                    @: <option value="@item.Name" selected="selected">@item.Name</option>
                    compval=@item.Name;
                  }
                  else
                  {
                    @: <option value="@item.Name">@item.Name</option>
                  }
                 i++;
                }
                
                }
       </select>
     

       var node = Model.Children.Where("Name = @0", compval);
      
       if(@node.Count == 1)
       {
     
         dynamic link = Library.MediaById(@node.competitionThumbImage);
         dynamic link2 = Library.MediaById(@node.competitionImage);
       }

       
    }

  • Rodion Novoselov 694 posts 858 karma points
    Feb 28, 2012 @ 15:49
    Rodion Novoselov
    0

    Hi. Is this what you want?

    @using umbraco.BusinessLogic;
    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNode
    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{
      var children = Model.Children.OrderBy("CreateDate desc");
      var first = children.First();
      var other = children.Skip(1);
    }

    <select id="ddlcompetition">
      <option value="@first.Name" selected="selected">
        @first.Name
      </option>
      @foreach(var item in other) {
         <option value="@item.Name">
            @item.Name
         </option>                          
      }
    </select>

    @{
      var node = children.Where("Name = @0", first.Name).FirstOrDefault();
      if(node != null) {
        var link = Library.MediaById(n.competitionThumbImage);
        var link2 = Library.MediaById(n.competitionImage);
      }
    }
  • hetaurhet 245 posts 267 karma points
    Feb 29, 2012 @ 12:22
    hetaurhet
    0

    yes... I wanted this.. but now I need to compare in where condition by ignoring case in below statement... means toLower() to be used for Name property value and first.Name value.

    varnode =children.Where("Name = @0", first.Name).FirstOrDefault();

  • Jeroen Breuer 4817 posts 11905 karma points admin mvp c-trib
    Feb 29, 2012 @ 12:45
    Jeroen Breuer
    0

    Be aware that .where has some performance problems: http://our.umbraco.org/forum/developers/razor/28479-Razor-menu-performance-(v4)

    Jeroen

  • Rodion Novoselov 694 posts 858 karma points
    Feb 29, 2012 @ 14:57
    Rodion Novoselov
    0

    Changing

    ...Where("Name == @0", first.Name)...

    to 

    ...Where("Name.ToLower() == @0", first.Name.ToLower())...

    should work, I suppose.

    (Not sure absolutely - there're limitations on using methods in dynamic LINQ, but as far as I remember for strings it's possible).

  • hetaurhet 245 posts 267 karma points
    Mar 01, 2012 @ 16:46
    hetaurhet
    0

    thank you ... it works... but now I need one more help... my razor code is as follows...

    @using umbraco.BusinessLogic;
    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNode
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
        //get selected options from Request.PathInfo

        
        string comp = (Request.QueryString["comp"]);
        
        string baseurl = Request.Url.GetLeftPart(UriPartial.Path);
    }
    <script type="text/javascript">
        String.prototype.LTrim = function()
          {
          return this.replace(/(^\s*)/g, "");
          }
                              
        $(document).ready(function () {

            $('#ddlcompetition').val('@comp');
            
            $("#ddlcompetition").change(function () { onSelectChange('@baseurl'); });


        });

        function onSelectChange(href) {

            var comp = $("#ddlcompetition option:selected").text().LTrim();
            $(window.location).attr('href', href + "?" + "comp=" + comp.toLowerCase());

            
        }
    </script>
    @{
      var children = Model.Children.OrderBy("CreateDate desc");
      var first = children.First();
      var other = children.Skip(1);
    }

    <select id="ddlcompetition" class="right">
      <option value="@first.Name" selected="selected">
        @first.Name
      </option>
      @foreach(var item in other)
     {
         <option value="@item.Name">
            @item.Name
         </option>                          
      }
    </select>
    <br />
    <div class="clear"></div>

    ... in above code you can see that I am looking for Query string parameter named 'comp'  and then setting value of 'select' input to querystring parameter value. But problem is that while passing parameter I convert it to lower case but dropdown contains value with uper case and lower case letters. So, I need to set value by comparing to_lower(parameter value ) and to_lower (select input value) ... how can I do that in script above?

Please Sign in or register to post replies

Write your reply to:

Draft