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?
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.
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 '@'?
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 :-)
I would like to add some informationthat might be nice to have if you at some point get in touch withRazorin a Umbracoversion4.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.
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?
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
I would add a few extra points (and some history) to Dennis's excellent comments
Thanks Dennis and Jeavon for these excellent, informative replies. A couple of extra specific questions:
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 '@'?
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:
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
Thanks again Dennis!
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
Thanks for the advice Jeavon :)
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:
And in a v6 you have to do it like this:
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
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
is working on a reply...