Copied to clipboard

Flag this post as spam?

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


  • Max Davidse 16 posts 37 karma points
    Jul 29, 2011 @ 10:55
    Max Davidse
    0

    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.

  • Alex 78 posts 136 karma points
    Jul 29, 2011 @ 11:01
    Alex
    2

    You are passing through @currentPage.videoCategory as string literal to the function, you need to evaluate that veriable to get the value,  try this:

    forach(var c in node.Children.Where("videoCategorie == \"" + currentPage.videoCategory + "\"))

     

  • Sebastiaan Janssen 5045 posts 15478 karma points MVP admin hq
    Jul 29, 2011 @ 11:04
    Sebastiaan Janssen
    0

    Unfortunately, I don't think there's a way to do this in the where. Of course you can work around this by doing:

    foreach (var c in node.Children) {
      if (c.videoCategorie == videoCategorie) {
        //do something
      }
    }

    Edit: of course, don't mind me, Alex is 100% correct. I was testing it wrongly. :-)

  • Max Davidse 16 posts 37 karma points
    Jul 29, 2011 @ 11:19
    Max Davidse
    0

    @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. 

  • Alex 78 posts 136 karma points
    Jul 29, 2011 @ 11:25
    Alex
    0

    @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))

     

  • Max Davidse 16 posts 37 karma points
    Jul 29, 2011 @ 11:30
    Max Davidse
    0

    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?

  • Max Davidse 16 posts 37 karma points
    Aug 05, 2011 @ 11:18
    Max Davidse
    0

    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!

     

Please Sign in or register to post replies

Write your reply to:

Draft