Copied to clipboard

Flag this post as spam?

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


  • Simeon Ostberg 123 posts 389 karma points
    Jan 08, 2020 @ 13:00
    Simeon Ostberg
    0

    Hi everyone,

    I have a blog running, which works with a macro. Now I only wanted to add another small post overview, but this doesn't work.

    Here is, what I have. First, this is the code which calls the 2 macros:

    <section class="section align-left">
    <div class="container col-sm-10">
            @Umbraco.RenderMacro("latestBlogposts",
                         new
                         {
                             numberOfPosts = Html.Raw(Umbraco.Field("noofposts").ToString()),
                             startNodeId = Model.Content.Id
                         })
    </div>
    <div class="container col-sm-2">
            @Umbraco.RenderMacro("latestBlogpostsmall",
                         new
                         {
                             numberOfPosts1 = Html.Raw(Umbraco.Field("noofposts").ToString()),
                             startNodeId1 = Model.Content.Id
                         })
    </div>
    </section>
    

    This is the first macro, which is working:

    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @using Umbraco.Web;
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{ 
        var startNodeId = Model.MacroParameters["startNodeId"] != null ? Model.MacroParameters["startNodeId"] : Model.Content.Id;
        var numberOfPosts = 100;
        if (Model.MacroParameters["numberOfPosts"] != null)
        {
            int.TryParse((string)Model.MacroParameters["numberOfPosts"], out numberOfPosts);
        }
     }
    @if (startNodeId != null)
    {
        @* Get the starting page *@
        var startNode = Umbraco.TypedContent(startNodeId);
        var blogposts = startNode.Children.OrderByDescending(x => x.CreateDate).Take(numberOfPosts);
    
        if (blogposts.Any())
        {
            foreach (ContentModels.Blogpost post in blogposts)
            {
            <section class="section align-center" style="padding: 40px 0 10px;">
                <div class="container">
                    <div class="col-sm-6 align-center img-column">
                        @if(post.BlogImage != null){
    
                            <a href="@post.Url"><img src="@post.BlogImage.Url" /></a>
                        }
                    </div>
    
                    <div class="col-sm-6 align-left">
                        <article style="max-width: 480px;">
                            <h5>@post.PageTitle</h5>
                            <small class="blogpost-date">@post.CreateDate.ToShortDateString()</small>                       
                            <p>@post.Excerpt</p>
                            <a href="@post.Url" class="btn btn-outline-clr" style="margin: 30px 0 0">Read more</a>
                        </article>
                    </div>
                </div>
            </section>
            }
        }
    }
    

    This is the second macro, which is throwing a YSOD with the message:

    Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: child

    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @using Umbraco.Web;
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{ 
        var startNodeId = Model.MacroParameters["startNodeId1"] != null ? Model.MacroParameters["startNodeId1"] : Model.Content.Id;
        var numberOfPosts = 100;
        if (Model.MacroParameters["numberOfPosts1"] != null)
        {
            int.TryParse((string)Model.MacroParameters["numberOfPosts1"], out numberOfPosts);
        }
     }
    @if (startNodeId != null)
    {
        @* Get the starting page *@
        var startNode = Umbraco.TypedContent(startNodeId);
        var blogposts = startNode.Children.OrderByDescending(x => x.CreateDate).Take(numberOfPosts);
    
        if (blogposts.Any())
        {
            foreach (ContentModels.Blogpost post in blogposts)
            {
            <section class="section align-center" style="padding: 0 !important;">
                <div class="container" style="padding: 0 !important;">
                    <div class="align-left vl">
                        <article style="margin-bottom: 20px !important;">
                            <a href="@post.Url"><h6 style="margin: 0 !important;">@post.PageTitle</h6>
                            <small class="blogpost-date">@post.CreateDate.ToShortDateString()</small></a>
                        </article>
                    </div>
                </div>
            </section>
            }
        }
    }
    

    With this line being marked red:

    @Umbraco.RenderMacro("latestBlogpostsmall",
    

    When I use the working "latestBlogposts", I have 2 blogs next to each other. I would be happy to get a hint.

    Thanks in advance, Simeon

Please Sign in or register to post replies

Write your reply to:

Draft