Copied to clipboard

Flag this post as spam?

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


  • Chris Halcrow 20 posts 39 karma points
    Oct 02, 2013 @ 05:45
    Chris Halcrow
    0

    What is @Model?

    What exactly does @Model refer to in Razor within Umbraco and what's the significance of the '@' in the syntax?  Is @Model something that's specific to MVC? Also what's the relationship between this and the 'DynamicNode' object?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 02, 2013 @ 08:09
    Dennis Aaen
    1

    Hi Chris,

    The @ symbol is used in Razor initiate code, and tell the compiler where to start interpreting code, instead of just return the contents of the file as text. Using a single character for this separation, results in cleaner, compact code which is easier to read.

    The razor documentation  can be found here:

    http://our.umbraco.org/documentation/Reference/Templating/Macros/Razor/basic-razor-syntax or here: http://our.umbraco.org/documentation/reference/templating/macros/razor/

    MVC documentation: http://our.umbraco.org/documentation/Reference/Templating/Mvc/

    @Model is comparable to the $currentPage in XSLT if you have any experience with XSLT. The @Model will contain all the data of the current page.

    So when you access a page on your site you can get data from that page with using the @Model.

    An example could be print the id of the page. It will look like this: @Model.Id

    I hope this post explaining what you were looking for.

    /Dennis

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Oct 02, 2013 @ 08:56
    Jeavon Leopold
    1

    I would add a few extra points (and some history) to Dennis's excellent comments

    • Officially Razor is a View Engine that is for use with Asp.Net Mvc 3 and above
    • Umbraco 4.6.0 introduced the Razor View Engine into its own Macro Engine so that it could be used with WebForms as Mvc was not supported
    • DynamicNode was introduced with Umbraco 4.7.2 as the Model when rendering a Razor script, it is a dynamic object of the current Umbraco Node
    • Umbraco 4.10.0 introduced full Mvc template support along with a new Model object IPublishedContent
    • IPublishedContent is strongly typed as @Model.Content but also represented as a dynamic object as @CurrentPage
    • Umbraco 6.0.0 added Macro Partial View support so that IPublishedContent can be used with WebForms templates as well as Mvc templates
    • In a future release Razor Macros and DynamicNode will be deprecated as they both have been superseded
  • Chris Halcrow 20 posts 39 karma points
    Oct 02, 2013 @ 22:08
    Chris Halcrow
    0

    Thanks Dennis and Jeavon for these excellent, informative replies.  A couple of extra specific questions:

    • So @Model obviously isn't as such connected to the the 'model' part of an MVC framework, right (other than that it can perhaps be considered a 'business object' that would ordinarily sit logically as part of the 'model' in MVC )?
    • The context in which I'm seeing @Model is this: 
        @inherits umbraco.MacroEngines.DynamicNodeContext
        @using ... (proprietary namespace)
    
    @{ var lang = @Model.AncestorsOrSelf("LanguageHomepage").First(); ... }

      So, I'm still unsure why I need to prefix Model with '@', since it's already in a code block that's delimited with an '@'?

     

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 02, 2013 @ 22:48
    Dennis Aaen
    1

    Hi Chris,

    If you allready have a code block with a @ you don´t need the before Model.

    So in conntext in which you are seeing you should be able to do someting like this:

    @inherits umbraco.MacroEngines.DynamicNodeContext
       
    @using...(proprietary namespace)


       
    @{
       
    var lang =Model.AncestorsOrSelf("LanguageHomepage").First();
       
    ...
       
    }

    Yeah I´m think you are right that the Model and DynamicNode are identical when used in the context of the current page.

    I hope you could use my post.

    /Dennis

     

  • Chris Halcrow 20 posts 39 karma points
    Oct 02, 2013 @ 23:45
    Chris Halcrow
    0

    Thanks again Dennis!

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Oct 02, 2013 @ 23:50
    Jeavon Leopold
    0

    Hi Chris,

    With the Mvc templating and IPublishedContent @Model can be the 'model' part from Mvc but you can also hydrate your own strongly typed models with data from the same objects. In fact, I have recently been contributing to a Mvc hybrid framework where you can combine the two approaches.

    Please forget DynamicNode ever existed and concentrate on using the Mvc templating and the strongly typed or dynamic model of IPublishedContent, DynamicNode will be gone before long :-)

    Jeavon

  • Chris Halcrow 20 posts 39 karma points
    Oct 03, 2013 @ 05:04
    Chris Halcrow
    0

    Thanks for the advice Jeavon :)

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 03, 2013 @ 08:26
    Dennis Aaen
    1

    Hi Chris,

    I would like to add some information that might be nice to have if you at some point get in touch with Razor in a Umbraco version 4.x. Umbraco v4 forgive @ in if statements in razor, but in v6 this are not forgiven.

    So in razor in a v4 of Umbraco you can do this:

    @if(@Model.Name == "home"){ 
    <p>This is the homepage!</p>
    }
    @if(@Model.NodeTypeAlias == "TextPage"){
    <p>this is a textpage</p>
    }else{
    <p>this ia NOT a textpage</p>
    }

    And in a v6 you have to do it like this:

    @if(Model.Name == "home"){ 
    <p>This is the homepage!</p>
    }
    @if(Model.NodeTypeAlias == "TextPage"){
    <p>this is a textpage</p>
    }else{
    <p>this ia NOT a textpage</p>
    }

    So in the first place you shouldn't have been able to write @ in the if statements in version 4, but in Razor 2.0 that's now enforced.

    I hope this little bonus infomation is useful.

    /Dennis

  • Jbennie 3 posts 71 karma points
    Jan 25, 2017 @ 17:36
    Jbennie
    0

    I found this guide to the problem ... as it wasn't clear

    http://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/

    Note the difference in access methods between UmbracoViewPage and UmbracoTemplatePage

Please Sign in or register to post replies

Write your reply to:

Draft