Copied to clipboard

Flag this post as spam?

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


  • ted 4 posts 24 karma points
    Apr 23, 2012 @ 18:07
    ted
    0

    filter by datatype

    Hi,

     

    I am building an hotel section for my website and I am using a datatype dropdown list with 1, 2, 3, 4 and 5 for the number of stars on each of the hotel pages.  I now need to filter by this on the hotel list page so that the user can click on 1 on the parent page and see all hotels which have 1 star.

    Is there a way to do this using razor?

    Thanks

  • Douglas Ludlow 210 posts 366 karma points
    Apr 23, 2012 @ 21:21
    Douglas Ludlow
    1

    Sure, you could use the Where method:

    dynamic oneStarHotels = Model.Children.Where("nameOfDDLProperty == 1");

    (Provided Model is the parent that contains the hotels.)

  • ted 4 posts 24 karma points
    Apr 24, 2012 @ 14:55
    ted
    0

    thanks for the reply.

    that shows me the list of hotels that are of a 1 star rating but what I need is a nav of 1 star, 2 star etc that when you click on the 1 star link it would then show the 1 star hotels

    Currently I have the code:

    <ul>
            @{
              var labels = Model.XPath("//Accommodation").OrderBy("Name").Where("rating == 4");;
              
              foreach(var label in labels)
              {
                 <li><a href="/">@label.Name</a></li>
              }
            }
        </ul>
  • Michael Latouche 504 posts 819 karma points MVP 3x c-trib
    Apr 24, 2012 @ 16:07
    Michael Latouche
    0

    Hi ted,

    Do you want this all in the same page? What you can do in Razor is get querystrings parameters, so you could populate your "star navigation" as links to the same page but with a querystring parameter indicating the star filter.

    Then in your razor macro, you can replace the hardcoded stars by the querystring parameter.

    If you want to build the navigation itself, then I think you would need something like this (pseudo-code, not tested):

    <ul>
            @{
              var ratings = Model.XPath("//Accommodation").Distinct("rating");
             
              foreach(var rating in ratings)
              {
                 <li><ahref="/mypage?starfilter=@rating">View @rating stars hotels</a></li>
              }
            }
        </ul>

    Hope this helps.

    Cheers,

    Michael.

  • ted 4 posts 24 karma points
    Apr 24, 2012 @ 18:24
    ted
    0

    unfortunately it threw back the error

    'umbraco.MacroEngines.DynamicNodeList' does not contain a definition for 'Distinct'

    and being more of a front end developer I am not sure what it needs?

     

    Ted

  • gilad 185 posts 425 karma points
    Apr 25, 2012 @ 10:19
    gilad
    0

    Hi ted.

    why dont you repeat the first solution 5 times?

    you can do it manually or with for loop :

    for (int i = 1; i < 6; i++)
            {
                dynamic hotels = Model.Children.Where("nameOfDDLProperty == " + i);
                <h2>@i stars hotels</h2>
                <ul>
                    @foreach (var item in hotels)
                    {
                       <li><a href="@item.Url">@item.Name</a></li> 
                    }  
                </ul>
            }  

    Hope that helps. Cheers

  • Michael Latouche 504 posts 819 karma points MVP 3x c-trib
    Apr 25, 2012 @ 10:48
    Michael Latouche
    0

    Hi ted,

    Sorry, I forgot Distinct did not work. By checking on this handy razor dynamic node cheat sheet (http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet), I saw that GroupBy works. This way you only show links to "stars" pages that have at least one hotel:

    <ul>
            @{
              var ratings = Model.XPath("//Accommodation").GroupBy("rating");
             
              foreach(var rating in ratings)
              {
                 <li><a href=/[email protected]>View @rating.Key stars hotels</a></li>
              }
            }
        </ul>

    Hope it works this time :-)

    Cheers,

    Michael.

Please Sign in or register to post replies

Write your reply to:

Draft