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.
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>
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>
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
Sure, you could use the Where method:
(Provided Model is the parent that contains the hotels.)
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:
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):
Hope this helps.
Cheers,
Michael.
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
Hi ted.
why dont you repeat the first solution 5 times?
you can do it manually or with for loop :
Hope that helps. Cheers
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:
Hope it works this time :-)
Cheers,
Michael.
is working on a reply...