Copied to clipboard

Flag this post as spam?

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


  • Robin Nicholl 137 posts 277 karma points
    Dec 01, 2011 @ 19:43
    Robin Nicholl
    0

    Simple(!!) test for boolean in uBlogsy/Razor

    having big trouble with this. I'm trying to only show posts that have a custom true/false property selected:

    foreach (DynamicNode in nodes{
    if @n.GetProperty("featureOnHomepage"== {
    @RenderPage("/macroScripts/uBlogsyShowPosts.cshtml"n.Id)
    }
    }

    But this doesn't work (nor do any of the loads of permutations I've tried)... 

    Robin

  • bob baty-barr 1180 posts 1294 karma points MVP
    Dec 01, 2011 @ 22:46
    bob baty-barr
    1

    i think you can just use the is or not syntax...

    if(@n.GetProperty("featureOnHomepage") {
    ...
  • Robin Nicholl 137 posts 277 karma points
    Dec 01, 2011 @ 23:09
    Robin Nicholl
    0

    I'd have though that should work, but it just gives an error when rendered to the page:

    Cannot implicitly convert type 'umbraco.interfaces.IProperty' to 'bool'
  • bob baty-barr 1180 posts 1294 karma points MVP
    Dec 01, 2011 @ 23:11
    bob baty-barr
    0

    so maybe

    if(@n.GetProperty("featureOnHomepage").AsBool) {
    ....
  • Robin Nicholl 137 posts 277 karma points
    Dec 01, 2011 @ 23:23
    Robin Nicholl
    0

    Oh, dear.. that gives an error on save:

    Cannot convert method group 'AsBool' to non-delegate type 'bool'. Did you intend to invoke the method?

    Tried adding parentheses to AsBool(), but that slo errored on save:

    'umbraco.interfaces.IProperty' does not contain a definition for 'AsBool' and the best extension method overload 'System.Web.WebPages.StringExtensions.AsBool(string)' has some invalid arguments

    ... and it didn't work as AsBool(@n.GetProperty("featureOnHomepage")) either...

  • bob baty-barr 1180 posts 1294 karma points MVP
    Dec 01, 2011 @ 23:29
    bob baty-barr
    0

    what about this page here... does that help?

    http://umbraco.com/follow-us/blog-archive/2011/3/1/umbraco-razor-feature-walkthrough-%E2%80%93-part-4

    looks like maybe we have to use true and false not 1 and zero??

  • bob baty-barr 1180 posts 1294 karma points MVP
    Dec 01, 2011 @ 23:31
  • bob baty-barr 1180 posts 1294 karma points MVP
    Dec 01, 2011 @ 23:35
    bob baty-barr
    0

    actually wait... i think i see what you need...

    i think you can put a where on your for each...

    @foreach(DynamicNode in nodes.Where("featureOnHomepage")) {
  • Robin Nicholl 137 posts 277 karma points
    Dec 02, 2011 @ 02:00
    Robin Nicholl
    0

    Thanks for thinking about this. Yes, this looks like it should work... but it gives an error on save:

    'System.Collections.Generic.IEnumerable' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable, System.Func)' has some invalid arguments

    Argh!

  • Robin Nicholl 137 posts 277 karma points
    Dec 02, 2011 @ 13:57
    Robin Nicholl
    0

    OK, finally got it to work like this:

    foreach (DynamicNode n in node) {
    if (@n.HasValue("featureOnHomePage")) {
    @RenderPage("/macroScripts/uBlogsyShowPosts.cshtml", n.Id)
    }
    }

    Could've sworn I tried this a few hundred attempts ago, but hey ho, at least it's working now!

    Robin

  • Robin Nicholl 137 posts 277 karma points
    Dec 03, 2011 @ 02:03
    Robin Nicholl
    0

    Damn, it doesn't work after all: it was just that I hadn't published some nodes since adding the "featureOnHomepage" property!

    Anybody have any ideas?

    Robin

  • Rasmus Berntsen 215 posts 253 karma points c-trib
    Dec 03, 2011 @ 02:16
    Rasmus Berntsen
    0

    Okay, so how much of the above code is working? What I see you're doing atm., is to check if n has a property of alias "featureOnHomePage" that has a value, but I don't see you actually check if that value is true or false?

    Edit: And in your first example, shouldn't it be true instead of 1? Or is that the custom part? :)

    It's much easier to help, if you pasted your current code and the error message you are getting. 

    Good luck!

  • Robin Nicholl 137 posts 277 karma points
    Dec 05, 2011 @ 01:53
    Robin Nicholl
    0
    The "HasValue" approach was obviously, with hindsight, never going to work... it's just that it seemed to because I hadn't published any of the non-qualifying pages since adding the property.But this *does* seem to work:
    foreach (DynamicNode in nodes{
    if @n.GetProperty("featureOnHomepage").Value=="1" {
    @RenderPage("/macroScripts/uBlogsyShowPosts.cshtml"n.Id)
    return;
    }
    }

    ... although, again, I'm pretty damn sure I tried this earlier on: must have had a typo or something!

  • Christian Rieß 24 posts 46 karma points
    Mar 27, 2012 @ 14:45
    Christian Rieß
    0

    I am experiencing a very similar error. I have two different Document types. Both Document Types have the property "displayInTopNavigation". I iterate through these documents in the same Razor script:

    @using umbraco.MacroEngines

    @{
      var languagePage = Model.AncestorOrSelf(1);
      <ul>
      @{
        foreach (var firstLevelPage in languagePage.Children)
        {
          try
          {
            if (firstLevelPage.NodeTypeAlias == "Folder" || firstLevelPage.NodeTypeAlias == "Page")
            {
              <div> @firstLevelPage.Name : @firstLevelPage.displayInTopNavigation.GetType() <br/</div>
              if (firstLevelPage.displayInTopNavigation)
              {
                <div> First Level Page </div>
              }
              else
              {
                <div>@firstLevelPage.Name not in top navigation</div>
              }
            }
          }
          catch (Exception e)
          {
            @e.Message<br/>
          }
        }
      }
      </ul>
    }



    The curious thing is, that the Razor script seems to interpret the
    "displayInTopNavigation" sometime as a System.Boolean, sometimes as a System.String. The <div> @firstLevelPage.Name : @firstLevelPage.displayInTopNavigation.GetType() <br/</div> statement puts out that the property is one time a boolean, one time a string. I am totally puzzled since I quadruple-checked the properties of both document types, I even let someone else check, and they are in fact "True/False".

    Could this be a bug or is there some subtle thing with Razor I didn't consider right?

    I am using umbraco 4.7.1.1

Please Sign in or register to post replies

Write your reply to:

Draft