Copied to clipboard

Flag this post as spam?

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


  • Pete 213 posts 285 karma points
    Aug 16, 2012 @ 12:48
    Pete
    0

    Check property from another document type?

    Hi all

    Quick razor question. I just want to check if a property "myproperty" ( a true/false ) has been enabled on a seperate document type to the one that the current page uses. I've currently got this but it does'nt seem to work :-/

    @foreach (var item in @Model.myotherdocumenttype) {
    if(string.IsNullOrEmpty(item._myproperty) == false) {
    <p>Yep it's been ticked!</p>
    }
    }

    Thanks!

  • Pete 213 posts 285 karma points
    Aug 16, 2012 @ 17:40
    Pete
    0

    Would have thought something as simple as below would work, but does'nt ?

    @{
      var parent = @Model.AncestorOrSelf("myotherdocumenttype");
      @Library.If(parent.testProp, "valueIfTrue", "valueIfFalse"
    }
  • Funka! 398 posts 661 karma points
    Aug 17, 2012 @ 00:40
    Funka!
    0

    Hi Pete,

    Since you are trying to look at a true/false property on another document altogether, my question is what is the relationship between these two? In your first post where you are looping through @Model.myotherdocumenttype, this seems to indicate that "myotherdocumenttype" is one of the direct children nodes (or could also be a property, like a Content Picker). But in your second post, you are looking for a parent/ancestor "myotherdocumenttype" , which scans in the opposite (upward) direction. Knowing how you want to find one document from the other would help a lot here.

    On a separate note, be careful when dealing with item._myproperty since it may not actually be a string and can give you a runtime error if it is actually a bool. In fact, when dealing with true/false properties in Umbraco, I've gotten in the habit of usually resorting to a compound condition, just to make sure the type is correct, such as the following:

    if (@item.myBooleanProperty.GetType() == typeof(bool) &(bool)item.myBooleanProperty)
    {
        <p>Yep it's been ticked!</p>
    }

    This is mainly to work around when "myBooleanProperty" really isn't a bool, but a string or DynamicNull or regular null or anything else...

    Best of luck!

     

     

  • Funka! 398 posts 661 karma points
    Aug 17, 2012 @ 00:41
    Funka!
    0

    Whoops had an extra "@" sign in there I didn't mean to, so adjust accordingly per context!

  • Pete 213 posts 285 karma points
    Aug 17, 2012 @ 12:43
    Pete
    0

    Hi Funka

    Thanks for the help, i'm starting to get my head round this now, first time i've used razor!

    so, i've now got the below, which loops round every document type and tells me if testprop has been set in those pages.

    @{
    var parent = @Model.AncestorOrSelf(1);
      foreach (var item in parent.Children.Where("testProp")) {
        if (@item.testProp.GetType() == typeof(bool) && (bool)item.testProp)
        {
            <p>Yep its been ticked!</p>
        }
      }
    }

    But what I really want to do is give change the @model to the specific name(alias) of the document type I want to look for the property in, ie filter @model.AncestorOrSelf(1) to only look for the umbTextpage.

    I've tried, @Model.umbTextpage, @Model.Textpage, @Model.AncestorOrSelf("umbTextpage"), @Model.AncestorOrSelf("Textpage") and none of which seem to work.

    Actually now i've found that doing this seems to filter the search to the doc type and property i'm looking for, sweet!

    @{
    var homeNode = @Model.AncestorOrSelf(1);
      foreach (var item in homeNode.Children.Where("testProp").Where("nodeTypeAlias == \"umbTextpage\"")) {
        if (@item.testProp.GetType() == typeof(bool) && (bool)item.testProp)
        {
            <p>Yep its been ticked!</p>
        }
      }
    }

    So two questions.

    1) Is this the best way to look for doc type with property?

    2) Do you always have to do a search for a foreach, can you not just use something like:
    @Library.If(homeNode.Id == Model.Id, "navSelected","") ?

    Pete

Please Sign in or register to post replies

Write your reply to:

Draft