Copied to clipboard

Flag this post as spam?

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


  • radmanmm 117 posts 108 karma points
    May 04, 2011 @ 16:11
    radmanmm
    0

    Accessing currentPage/Property from razor

    Hello All,

    Seems like an obvious question, but I can seem to find any references to What model is, Root, currentPage, what?

    I need to see if the currentPage has a propery set to do some branching logic. I was assuming that Model represents currentPage, but I am not 100% sure.  I have tried:

    Model.property == value

    Model.GetProperty("propertyAlias") == value

    My guess is that because it is a Dynamic Node, I need to some how cast it or tell the engine what concrete type it is, but I am not sure.

    The property I am trying to access is in a base document type that all my other document types inherit from so there will not be a case where that property won't exist.

    Any help would be appreciated.

    Thanks for your time.

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    May 04, 2011 @ 17:58
    Dirk De Grave
    0

    Model is indeed equivalent to the $currentPage param in xslt.

    using Model.propertyAlias should get you the value of the object, if you're using Model.GetProperty("propertyAlias").Value, you'll get a string.

    Be aware tho, using Model.propertyAlias may be casted to a different type than you'd expect...

    For example, if I'd had a property with a string value of "3-4", Model.propertyAlias may evaluate this as a date, so you'd need to use Model.GetProperty("propertyAlias").Value to get the actual value you're expecting (or maybe after casting it to a type you're expecting)

     

    Cheers,

    /Dirk

  • radmanmm 117 posts 108 karma points
    May 04, 2011 @ 21:38
    radmanmm
    0

    Thanks Dirk for the response.  I tried that but get the following error:

    Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'umbraco.MacroEngines.DynamicNode' does not contain a definition for 'seoRemoveHeader'

     

    The code I am using looks like this:

    @if (Model.seoRemoveHeader != 1) {

           //do something

    }

  • Mads Krohn 211 posts 504 karma points c-trib
    May 05, 2011 @ 22:15
    Mads Krohn
    0

    The error message you are getting is a bit strange to be honest. As Dirk confirmed, Model is the current node and is a dynamic of the type DynamicNode.

    When you try to get a property on the node as the example you provided, Model.seoRemoveHeader, you should either get the value of that property or a DynamicNull object. You should never get an exception. 

    If you try to compare a property to some other value, like your example, Model.seoRemoveHeader != 1, there should be three possible outcomes.

    • The seoRemoveHeader value is of the type int, and the comparison succedes.
    • The seoRemoveHeader value is of another type than int, and you get an apropriate exception for that.
    • The seoRemoveHeader property does not exist, and you get an exception like:
      Operator '!=' cannot be applied to operands of type 'umbraco.MacroEngines.DynamicNull' and 'int'
    What version of Umbraco are you running, and have you in any way altered the Model object in code not shown here?

     

  • radmanmm 117 posts 108 karma points
    May 05, 2011 @ 22:17
    radmanmm
    0

    I am using umbraco 3.6.1 which looks like it uses razor differently, is that the problem?

  • Mads Krohn 211 posts 504 karma points c-trib
    May 05, 2011 @ 22:20
    Mads Krohn
    0

    I'm guessing you mean Umbraco 4.6.1 ? I believe that Umbraco 4.6.1 had a quite experimental implementation of Razor, A LOT has changed from then to 4.7, I would highly recommend you to upgrade your site, if that is in anyway possible. I'm afraid I can't assist you much on the 4.6.1 implementation.

  • radmanmm 117 posts 108 karma points
    May 05, 2011 @ 22:22
    radmanmm
    0

    yeah sorry 4.6.1, ok thanks for your help anyway.

Please Sign in or register to post replies

Write your reply to:

Draft