Umbraco 4.7: Issue with foreach checking against page variable.
Hey all,
I have a pretty basic Razor script for looking all videos under a node based on a parameter. Added to that, I want to check them to a setting for that page.
Each page will get a videoCategory assigned to it. When the page is loaded, the script will loop through all videos, and checks the category against the category assigned to the video.
To do this, I created the following piece of code.
var currentPage = Model; dynamic node = new DynamicNode(@Parameter.ParentID); <div>@currentPage.videoCategory</div> foreach (var c in node.Children.Where("videoCategorie == @currentPage.videoCategory"))
For some reason, in the div above the foreach loop, razor has no problem telling me which videoCategory is assigned to currentPage, but in the foreach loop, it gives an error:
Error loading Razor Script GetYouTubeFans.cshtml No property or field 'videoCategory' exists in type 'Func`2'
I have tried this using @currentPage.videoCategory and without the @, none of them work. What am I doing wrong here?
@Alex, if I try your sollution, it gives a different error:
Unterminated string literal. Strings that start with a quotation mark (") must be terminated before the end of the line. However, strings that start with @ and a quotation mark (@") can span multiple lines.
I've been toying around with it, and you almost had it, but exactly the other way around. Now I don't get an error...but it doesn't work as I want it to...but I have something to work with now. What I made of it is this:
foreach (var c in node.Children.Where("videoCategorie == \" + currentPage.videoCategory + \""))
@Sebastiaan, yikes. I was hoping to avoid this sort of workarounds.
@Max: Oops missed off a trailing double quote from my example. What type is videoCategory? I'm not sure your code will work as it is still trying to pass currentPage.videoCategory as a string literal.
if videoCategory is a string then this should work
forach(var c in node.Children.Where("videoCategorie == \"" + currentPage.videoCategory + "\""))
if it is a int then you don't need the enclosing escaped double quotes so this should work
forach(var c in node.Children.Where("videoCategorie == " + currentPage.videoCategory))
currentPage.videoCategory is a datatype dropdown which consists of strings. For some strange reason it still doesn't display the results, but the errors have been ditched.
Gonna check to see what's the reason of it. Because it worked when I just checked it using:
foreach (var c in node.Children.Where("videoCategorie == \"Fans\"")) and somehow doesn't when checking it against the variable. Does that need to be somehow converted to an actual string since it comes from a dropdown?
I ended up rewriting part of it. Because eventhough the syntax of the foreach look was correct, it still wouldn't trigger a correct result, very strange behaviour this.
So I rewrote it to:
foreach(var c in node.Children.OrderBy("id")) { if (c.videoCategorie == currentPage.videoCategory)
Umbraco 4.7: Issue with foreach checking against page variable.
Hey all,
I have a pretty basic Razor script for looking all videos under a node based on a parameter. Added to that, I want to check them to a setting for that page.
Each page will get a videoCategory assigned to it. When the page is loaded, the script will loop through all videos, and checks the category against the category assigned to the video.
To do this, I created the following piece of code.
var currentPage = Model;
dynamic node = new DynamicNode(@Parameter.ParentID);
<div>@currentPage.videoCategory</div>
foreach (var c in node.Children.Where("videoCategorie == @currentPage.videoCategory"))
For some reason, in the div above the foreach loop, razor has no problem telling me which videoCategory is assigned to currentPage, but in the foreach loop, it gives an error:
Error loading Razor Script GetYouTubeFans.cshtml
No property or field 'videoCategory' exists in type 'Func`2'
I have tried this using @currentPage.videoCategory and without the @, none of them work. What am I doing wrong here?
Thanks in advance,
Max.
You are passing through @currentPage.videoCategory as string literal to the function, you need to evaluate that veriable to get the value, try this:
Unfortunately, I don't think there's a way to do this in the where. Of course you can work around this by doing:
Edit: of course, don't mind me, Alex is 100% correct. I was testing it wrongly. :-)
@Alex, if I try your sollution, it gives a different error:
Unterminated string literal. Strings that start with a quotation mark (") must be terminated before the end of the line. However, strings that start with @ and a quotation mark (@") can span multiple lines.
I've been toying around with it, and you almost had it, but exactly the other way around. Now I don't get an error...but it doesn't work as I want it to...but I have something to work with now. What I made of it is this:
foreach (var c in node.Children.Where("videoCategorie == \" + currentPage.videoCategory + \""))
@Sebastiaan, yikes. I was hoping to avoid this sort of workarounds.
@Max: Oops missed off a trailing double quote from my example. What type is videoCategory? I'm not sure your code will work as it is still trying to pass currentPage.videoCategory as a string literal.
if videoCategory is a string then this should work
if it is a int then you don't need the enclosing escaped double quotes so this should work
currentPage.videoCategory is a datatype dropdown which consists of strings. For some strange reason it still doesn't display the results, but the errors have been ditched.
Gonna check to see what's the reason of it. Because it worked when I just checked it using:
foreach (var c in node.Children.Where("videoCategorie == \"Fans\"")) and somehow doesn't when checking it against the variable. Does that need to be somehow converted to an actual string since it comes from a dropdown?
I ended up rewriting part of it. Because eventhough the syntax of the foreach look was correct, it still wouldn't trigger a correct result, very strange behaviour this.
So I rewrote it to:
foreach(var c in node.Children.OrderBy("id"))
{
if (c.videoCategorie == currentPage.videoCategory)
and that DID work strangely enough.
Anyway, kudo's for helping me out!
is working on a reply...