I'm using Umbraco version 6.0.0 and I would like to get a reference to the current node directly in a template (as is possible in a razor macro). For example, to List Child Pages From Current Page.
Now how do I use Model.Content in a template to loop through child nodes and display their properties? I've tried everything I can think of and haven't guessed the magic combination yet.
If you could post a little sample code, that would be great.
Get current node directly in a template (v6)
Hello Umbraco Colleagues,
I'm using Umbraco version 6.0.0 and I would like to get a reference to the current node directly in a template (as is possible in a razor macro). For example, to List Child Pages From Current Page.
In a razor macro, we can do this:
However, I only need this in one view. Seems like overkill to create a razor script then a macro then insert the macro in a view.
But if I use the above razor code directly in a view, I get this error:
The view at '~/Views/productListing.cshtml' must derive from WebViewPage, or WebViewPage<TModel>
and sometimes this...
Foreach cannot operate on a 'method group'. Did you intend to invoke the 'method group'?
The main issue seems to be about using the word Model directly in a view.
It would be great to be able to do in a template the same things we can do in a macro.
Any insight is greatly appreciated?
David
In v6, you can inherits from Umbraco.Web.Mvc.UmbracoTemplatePage for your template, and use "Model.Content" to get access to your current node.
Is this what you want?
Hi Owen,
That's great. Thanks.
Now how do I use Model.Content in a template to loop through child nodes and display their properties? I've tried everything I can think of and haven't guessed the magic combination yet.
If you could post a little sample code, that would be great.
Thanks.
You're using MVC mode I take it? This should work just fine:
Thanks, Seb.
That's it exactly. H5YR
Great! Most of the Razor implementation is the same or very similar in MVC templates, so make sure to play with what you're already familiar with. :)
is working on a reply...