Copied to clipboard

Flag this post as spam?

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


  • Sonja 133 posts 621 karma points
    Sep 20, 2018 @ 11:23
    Sonja
    0

    problem setting blog from the demo

    Hi, I'm trying to insert the blog from the demo page into my page and I'm getting NullReferenceException on the partial view for the latest blogposts:

    System.NullReferenceException
      HResult=0x80004003
      Message=Object reference not set to an instance of an object.
      Source=App_Web_latestblogposts.cshtml.d673bf47.lelqw2sz
      StackTrace:
       at ASP._Page_Views_MacroPartials_LatestBlogposts_cshtml.Execute() in LatestBlogposts.cshtml:line 19
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
       at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
       at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer)
       at Umbraco.Web.Mvc.ControllerExtensions.RenderViewResultAsString(ControllerBase controller, ViewResultBase viewResult)
       at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, IPublishedContent content)
       at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, INode node)
       at umbraco.macro.LoadPartialViewMacro(MacroModel macro)
       at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId)
    

    and the macro partial view is:

    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 = 3;
        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);
        //Gets all blogposts to calculate pages
        var blogposts = startNode.Children.OrderByDescending(x => x.CreateDate).ToList();  //CRASH HERE BECAUSE STARTNODE=NULL
        var pageCount = (int)Math.Ceiling((double)blogposts.Count / (double)numberOfPosts);
        //gets the page from the querystring and sets it to one if it is out of range
        var page = 1;
        if (!string.IsNullOrEmpty(Request.QueryString["page"]))
        {
            int.TryParse(Request.QueryString["page"], out page);
            if (page <= 0 || page > pageCount)
            {
                page = 1;
            }
        }
        //Gets the blogposts for the current page
        var pagedBlogposts = blogposts.Skip((page - 1) * numberOfPosts).Take(numberOfPosts).ToList();
    
        if (pagedBlogposts.Count > 0)
        {
            <div class="blogposts">
    
                @foreach (ContentModels.Blogpost post in pagedBlogposts)
                {
                    <a href="@post.Url" class="blogpost">
                        <div class="blogpost-meta">
                            <small class="blogpost-date">@post.CreateDate.ToShortDateString()</small>
                            <small class="blogpost-cat">
                                @Html.Partial("~/Views/Partials/CategoryLinks.cshtml", post.Categories)
                            </small>
                        </div>
                        <h3 class="blogpost-title">@post.PageTitle</h3>
                        <div class="blogpost-excerpt">@post.Excerpt</div>
                    </a>
                }
            </div>
        }
    
        if (blogposts.Count > numberOfPosts)
        {
            <div class="pagination">
                <nav class="nav-bar nav-bar--center">
                    @if (page <= 1)
                    {
                        <span class="nav-link nav-link--black nav-link--disabled">Prev</span>
                    }
                    else
                    {
                        <a class="nav-link nav-link--black" href="@(Model.Content.Url + "?page=" + (page - 1))">Prev</a>
                    }
    
                    @for (int i = 1; i <= pageCount; i++)
                    {
                        <a class="nav-link nav-link--black @(page == i ? "nav-link--active" : null)" href="@(Model.Content.Url + "?page=" + i)">@i</a>
                    }
                    @if (page == pageCount)
                    {
                        <span class="nav-link nav-link--black nav-link--disabled">Next</span>
                    }
                    else
                    {
                        <a class="nav-link nav-link--black" href="@(Model.Content.Url + "?page=" + (page + 1))">Next</a>
                    }
    
                </nav>
            </div>
        }
    }
    

    It comes that var startNode = Umbraco.TypedContent(startNodeId); is returning null. Please help

  • Sonja 133 posts 621 karma points
    Sep 20, 2018 @ 11:52
    Sonja
    100

    solved it. The blog page should have been published first

Please Sign in or register to post replies

Write your reply to:

Draft