Copied to clipboard

Flag this post as spam?

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


  • mikkel 143 posts 365 karma points
    Feb 11, 2019 @ 13:18
    mikkel
    0

    Why does my code not work

    I want to show all my project on one page. but it give me a null refence everytime i use the code to show all project. But when i add .Take(6) it works :D i dont get it. It worked earlier today. What do i do wrong ??

    This one here it work with

     var selection = Model.Content.Site().FirstChild("projekter").Children().Where(x => x.IsVisible()).OrderByDescending(x => x.CreateDate).Take(6);
    

    But this one her i dont. I get a nul reference

    var blogItems = Model.Content.Children<Projekt>();
    

    Here is the code i want to have to work :D

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    
    @{
       var blogItems = Model.Content.Children<Projekt>();
    
    }
    
    
    
    <!-- Portfolio Grid -->
    <section class="bg-light" id="portfolio">
        <div class="container">
            <div class="row">
    
                <div class="col-lg-12 text-center">
                    <h2 class="section-heading text-uppercase">Projekter</h2>
                    <h3 class="section-subheading text-muted">Vores projekter som vi er meget stolte af at vise dig.</h3>
                </div>
            </div>
            <div class="row">
    
                @foreach (var item in blogItems)
                {
    
                    <div class="col-md-4 col-sm-6 portfolio-item">
                        <a class="portfolio-link" data-toggle="modal" href="@item.Url">
                            <div class="portfolio-hover">
                                <div class="portfolio-hover-content">
                                    <i class="fas fa-plus fa-3x"></i>
                                </div>
                            </div>
    
                            <img src="@Umbraco.TypedMedia(item.GetPropertyValue<int>("billedeProjekt")).Url" style="width: 350px; height: 262px;" />
    
    
                        </a>
                        <div class="portfolio-caption">
                            <h4>@item.Name</h4>
                            <p class="text-muted">@Umbraco.Field("navnPaaProjekt")</p>
                        </div>
                    </div>
    
                }
            </div>
    
    
        </div>
    </section>
    
  • Rhys Hamilton 140 posts 942 karma points
    Feb 11, 2019 @ 15:22
    Rhys Hamilton
    100

    The reason you're getting a null reference using Model.Content.Children<Projekt>() is because you're trying to return the children for the current page, instead of returning all of the children of the Projekt document type.

    If the current page doesn't have any children of type 'projekter', then you'll always return a null value.

    Adapting your original solution (that works), you could try using the following line instead:

    var blogItems = Model.Content.Site().FirstChild("projekter").Children();
    

    It's a bit shorter and removes the additional logic that you're working solution has, so hopefully a happy medium!

  • Rhys Hamilton 140 posts 942 karma points
    Feb 11, 2019 @ 15:25
    Rhys Hamilton
    0

    You might also want to look at the documentation on querying as well :)

  • mikkel 143 posts 365 karma points
    Feb 13, 2019 @ 07:34
    mikkel
    0

    Thanks for your help that work fine :D

Please Sign in or register to post replies

Write your reply to:

Draft