Using "if item.propertyValue" to filter a selection
Hi all.
I have a selection generated from the children of the current page.
Within that selection (let's call it Fruit) I have items and each item has a fruitType.
This code doesn't work:
@{
var selection = CurrentPage.Children("fruit").Where("Visible");
}
<ul>
@foreach(var item in selection){
@if(@item.fruitType == "Apple");{
<li>
<a href="@item.Url">@item.Name</a><br/>
@item.fruitName<br>
@item.fruitType<br>
@if (item.image != null && !(item.image is Umbraco.Core.Dynamics.DynamicNull))
{ var m = Umbraco.Media(item.image);
<img src="@m.Url" alt="Picture of @item.Name" />
}
</li>
}
}
</ul>
What I'm trying to do is to only list the items with a fruitType of "Apple".
This value is selected from a dropdown list so I'm not sure if umbraco is storing this as the string "Apple" or as a numeric value.
So... I checked what the option values were for the fruitType dropdown list and they're numeric, but substituting the numeric value made no difference.
I've also tried several different permutations including just including this within the "forEach" routine:
@if(item.fruitType != null)
{**********}
Still not working :(
Starting to wonder whether or not this is something to do with the fact that I'm using Dropdown list to store the fruitType...
@{
var selection = CurrentPage.Children("fruit").Where("Visible");
}
<ul>
@foreach(var item in selection){
if (item.fruitType == "Apple") { /// I REMOVED THE @ AND ; HERE
<li>
<a href="@item.Url">@item.Name</a><br/>
@item.fruitName<br>
@item.fruitType<br>
@if (item.image != null && !(item.image is Umbraco.Core.Dynamics.DynamicNull))
{ var m = Umbraco.Media(item.image);
<img src="@m.Url" alt="Picture of @item.Name" />
}
</li>
}
}
</ul>
I agree with Søren. When you're using the @ helper to generate C# code on a view, you only need one "@" to begin the code branch.
Søren's example should work as he starts the foreach (C# code) with an @ helper, then the following code on the next line down is an "if" statement, which is also C# - therefore you don't need to add another @ helper.
If the li tag was above the if statement, then you would need another @ helper for the "if" statement - example below:
<ul>
@foreach(var item in selection){
<li>
@if (item.fruitType == "Apple") {
<a href="@item.Url">@item.Name</a><br/>
@item.fruitName<br>
@item.fruitType<br>
@if (item.image != null && !(item.image is Umbraco.Core.Dynamics.DynamicNull))
{ var m = Umbraco.Media(item.image);
<img src="@m.Url" alt="Picture of @item.Name" />
}
}
</li>
}
</ul>
Thanks Søren, I'm a little confused by the lack of clear distinction between code and text with Razor. It's not like ASP, JSP or PHP where code blocks are clearly wrapped in specific tags.
I just assumed that the @ symbol was alway required for functional blocks.
Thanks also to Alex for clearing that misconception up.
Just out of curiousity though, is there an "Umbraco Razor" Cookbook for common code use cases out there?
Using "if item.propertyValue" to filter a selection
Hi all.
I have a selection generated from the children of the current page. Within that selection (let's call it Fruit) I have items and each item has a fruitType.
This code doesn't work:
What I'm trying to do is to only list the items with a fruitType of "Apple". This value is selected from a dropdown list so I'm not sure if umbraco is storing this as the string "Apple" or as a numeric value.
Any suggestions?
So... I checked what the option values were for the fruitType dropdown list and they're numeric, but substituting the numeric value made no difference.
I've also tried several different permutations including just including this within the "forEach" routine:
Still not working :(
Starting to wonder whether or not this is something to do with the fact that I'm using Dropdown list to store the fruitType...
bump still not able to get this working... can anyone offer some insight on how to filter selection results based on a choice from a select list?
Still not working... I tried a few variations on a solution I found here:
https://our.umbraco.org/forum/developers/razor/19087-Using-dropdown-values-in-Where-statement-with-razor
but none work.
It's odd that as soon as I add a simple @If conditional to the mix then it all goes pear shaped.
Whats your error message?
To me it seems like you have a few too many @'s
I agree with Søren. When you're using the @ helper to generate C# code on a view, you only need one "@" to begin the code branch.
Søren's example should work as he starts the foreach (C# code) with an @ helper, then the following code on the next line down is an "if" statement, which is also C# - therefore you don't need to add another @ helper.
If the li tag was above the if statement, then you would need another @ helper for the "if" statement - example below:
Thanks Søren, I'm a little confused by the lack of clear distinction between code and text with Razor. It's not like ASP, JSP or PHP where code blocks are clearly wrapped in specific tags.
I just assumed that the @ symbol was alway required for functional blocks.
Thanks also to Alex for clearing that misconception up.
Just out of curiousity though, is there an "Umbraco Razor" Cookbook for common code use cases out there?
Hi Colm,
You can find some Umbraco TV chapters about Razor and Razor syntax.
https://umbraco.tv/videos/umbraco-v7/implementor/working-with-umbraco-data/razor-syntax/introduction-to-razor/
https://umbraco.tv/videos/umbraco-v7/implementor/working-with-umbraco-data/querying-umbraco-data-with-razor/currentpage-object/
Hope this helps,
/Dennis
I have a category document type which creates child node of books document type.
I want to show on my web page every catname that includes all books that belongs to that category.
Like cat1 --> Book1 Book2 Book3
cat2 --> Book4 Book5 cat3 --> ......... so on....
is working on a reply...