Copied to clipboard

Flag this post as spam?

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


  • Lance Swan 3 posts 73 karma points
    Feb 18, 2016 @ 17:05
    Lance Swan
    0

    Count Function Within Macro Partial View

    I am trying to use the Count() function on a set of Children pages but am receiving an "Error loading Partial View" error. I'd like to set the Count() as a variable so I may use it in my html output. This is the code I have within the editor in Partial View Macro Files.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var selection = CurrentPage.Children.Where("Visible").OrderBy("CreateDate desc");
        var selectioncount = selection.Count();
    }
    
    <h3>There are @selectioncount items</h3>
    @foreach (var item in selection)
    {
    <div class="row">
        <a href="@item.Url" title="Read @item.articleTitle"><img src="@Umbraco.Media(@item.articleImage).Url" alt="Crosley Radio"/></a>
        <div>
            <div>
                @Umbraco.RenderMacro("displayAuthor", new {authorName = @item.author})
                <h3>@item.articleTitle</h3>
                <span class="meta">@item.CreateDate.ToString("MMMM dd, yyyy") | Category</span>
                <p>@Umbraco.Truncate((@item.articleBody),100)</p>
                <a class="readmore" href="@item.Url">Read More..</a>
            </div>
        </div>
    </div>
    <hr />
    }
    

    If I remove the selectioncount var and h3 line, the page renders fine, so it is clearly the Count() function that is giving me the error. I'm sure I am overlooking something simple as I am new to Umbraco. Thanks!

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Feb 18, 2016 @ 17:51
    Dennis Aaen
    0

    Hi Lance and welcome to our :-)

    Could you please try the code snippet below and see if this works for you.

    @{
        var selection = CurrentPage.Children.Where("Visible").OrderBy("CreateDate desc");
        var selectioncount = selection.Count();
    }
    
    <h3>There are @selectioncount items</h3>
    @foreach (var item in selection)
    {
    <div class="row">
        <a href="@item.Url" title="Read @item.articleTitle">
        <img src="@Umbraco.Media(item.articleImage.ToString()).Url" alt="Crosley Radio"/></a>
        <div>
            <div>
                @Umbraco.RenderMacro("displayAuthor", new {authorName = @item.author})
                <h3>@item.articleTitle</h3>
                <span class="meta">@item.CreateDate.ToString("MMMM dd, yyyy") | Category</span>
                <p>@Umbraco.Truncate((@item.articleBody),100)</p>
                <a class="readmore" href="@item.Url">Read More..</a>
            </div>
        </div>
    </div>
    <hr />
    }
    

    Hope this helps,

    /Dennis

  • Lance Swan 3 posts 73 karma points
    Feb 18, 2016 @ 18:12
    Lance Swan
    0

    Thanks for your help. Your snippet yielded the same error. In order to simplify my troubleshooting, I cannot even get this snippet to properly output the Count().

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var selection = CurrentPage.Children.Where("Visible").OrderBy("CreateDate desc");
        var selectioncount = selection.Count();
    }
    
    <h3>There are @selectioncount items</h3>
    

    Any other ideas?

  • Kin 30 posts 172 karma points
    Feb 19, 2016 @ 16:26
    Kin
    0

    I just tried your simplified snippet on 7.3.8 and it didn't yield any errors.

    Can you post the code of the page where you call your macro?

Please Sign in or register to post replies

Write your reply to:

Draft