Copied to clipboard

Flag this post as spam?

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


  • David F. Hill 122 posts 242 karma points
    Feb 26, 2013 @ 04:10
    David F. Hill
    0

    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:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    <ul>           
      @foreach (var childPage in Model.Children.Where("Visible"))
      {
      <li>
      <a href="@childPage.Url">@childPage.Name</a>
      </li>
      }
    </ul>

    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

  • Owen 123 posts 246 karma points
    Feb 26, 2013 @ 04:35
    Owen
    1

    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?

  • David F. Hill 122 posts 242 karma points
    Feb 26, 2013 @ 16:29
    David F. Hill
    0

    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.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Feb 26, 2013 @ 16:40
    Sebastiaan Janssen
    1

    You're using MVC mode I take it? This should work just fine:

    @foreach (var childPage in CurrentPage.Children().Where("Visible"))
                      {
                        <a href="@childPage.Url">@childPage.Name</a>
                      }
    
  • David F. Hill 122 posts 242 karma points
    Feb 26, 2013 @ 16:59
    David F. Hill
    0

    Thanks, Seb.

    That's it exactly. H5YR

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Feb 26, 2013 @ 17:00
    Sebastiaan Janssen
    0

    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. :)

Please Sign in or register to post replies

Write your reply to:

Draft