Copied to clipboard

Flag this post as spam?

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


  • Dustin 15 posts 105 karma points
    Dec 21, 2015 @ 20:04
    Dustin
    0

    Switching from CurrentPage to Model.content

    I have this simple partial which pulls some information about an application. I am starting to use visual studio instead of developing in the back office. How would I convert this to use model.content?

    I have tried different variations of:

    var selection = Model.Content.FirstChild("ApplicationDatabase").Children.Where("Visible").Take(4);
    

    This is the code:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    
        @{
            var selection = CurrentPage.Site().FirstChild("ApplicationDatabase").Children.Where("Visible").Take(4);
        }
    
        <div class="container">
            <!-- Page Features -->
            <div class="row text-center">
            @foreach(var item in selection){
                    <div class="col-md-3 col-sm-6 hero-feature">
                        <div class="thumbnail">
                            <img src="@Umbraco.Media(item.ApplicationImage).Url" alt="">
                            <div class="caption">
                                <h3>@item.ApplicationName</h3>
                                <p>@item.ApplicationDescription</p>
                                <p>
                                    <a href="@item.applicationLinks" class="btn btn-primary">Open</a> <a href="@item.url" class="btn btn-default">More Info</a>
                                </p>
                            </div>
                        </div>
                    </div>
            }        
        </div>
    
  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Dec 21, 2015 @ 21:52
    Marc Goodson
    100

    Hi Dustin

    CurrentPage.Site() is a shortcut to get the current homepage when using the dynamic CurrentPage approach.

    With Model.Content you have two options; use the UmbracoHelper methods:

    https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/

    soTypedContentAtRoot will give you an IEnumerable of IPublishedContent of the root nodes in your Umbraco installation, so you can use this to find your site root node to query off

    eg:

    var siteRoot = Umbraco.TypedContentAtRoot().FirstOrDefault();
    if (siteRoot != null){
        var selection = siteRoot.FirstChild(f=>f.Name=="ApplicationDatabase").Children.Where(f => f.IsVisible()).Take(4);
    }
    

    or you can use AncestorOrSelf on Model.Content to interate up to the top level of your site (1 is the top level)

    eg:

    var siteRoot = Model.Content.AncestorOrSelf(1)
    if (siteRoot != null){
        var selection = siteRoot.FirstChild(f=>f.Name=="ApplicationDatabase").Children.Where(f => f.IsVisible()).Take(4);
    }
    

    Also take a look at the strongly typed razor cheat sheet, very handy when making the switch in syntax....

    https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets

  • Dustin 15 posts 105 karma points
    Dec 23, 2015 @ 21:46
    Dustin
    0

    Thank you very much for your help. That was just what I needed to get to the next level!

Please Sign in or register to post replies

Write your reply to:

Draft