Copied to clipboard

Flag this post as spam?

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


  • Chris Currie 4 posts 84 karma points
    Oct 08, 2018 @ 08:20
    Chris Currie
    0

    Umbraco 7 Partial View Macro File (from snippet) is not showing values

    I'm following this tutorial: https://our.umbraco.com/documentation/tutorials/creating-basic-site/Articles-Parent-and-Article-Items

    Everything has gone to plan except my values are not showing.

    The @item.Name and @item.Url display, however the values I insert from the document template (i.e. @Umbraco.Field("articleTitle")) do not. No error... just no value is displayed.

    The article uses @item.ArticleContents which confused me as previously, the instructions requested.

    ...replace the Page field tags with the relevant properties e.g. articlesTitle and articlesBodyText for the Articles Main and the articleTitle and articleContents for Article Item

    The alias was therefore 'articleContents' - yet the code uses 'ArticleContents'

    If I attempt to use the @item approach (e.g. @item.articleTitle or @item.ArticleTitle) then I receive errors at runtime (Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'articleTitle' and no extension method 'articleTitle'). It is the same if I use 'ArticleTitle'.

    Here's the code.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Web
    
    
    @*
    This snippet makes a list of links to the of children of the current page 
    using an unordered HTML list.
    
    How it works:
    - It uses the Children method to get all child pages
    - It then uses the OrderByDescending() method, which takes the property to sort. In this case the page's creation date.
    - It then generates links so the visitor can go to each page
    *@
    
    @{ var selection = Model.Content.Children.Where(x => x.IsVisible()).OrderByDescending(x => x.CreateDate).ToArray(); }
    
    @if (selection.Length > 0)
    {
        @foreach (var item in selection)
        {
            <!-- blog article -->
              <article class="blog">
                <!-- main heading of the blog -->
                <h2><a href="@item.Url">@item.Name</a>@Umbraco.Field("articleTitle")</h2>
                <!-- image holder -->
                <div class="img-holder">
                  <img src="~/images/img11.jpg" srcset="images/img11-2x.jpg 2x" alt="Image Description" width="871" height="326">
                </div>
                <!-- menu items -->
                <ul class="menu-item">
                  <li><a href="#"><span class="icon icon-user"></span>@Umbraco.Field("author")</a></li>
                  <li><span class="icon icon-calendar"></span><time datetime="@Umbraco.Field("dateOfPublish", formatAsDate: true)">@Umbraco.Field("dateOfPublish", formatAsDate: true)</time></li>
                  <li><a href="#"><span class="icon icon-bubbles"></span>@Umbraco.Field("commentsQuantity") Quantity comments</a></li>
                </ul>
                <p>
                    @Umbraco.Field("bodyText")
                </p>
                <!-- btn holder -->
                <div class="btn-holder">
                  <a href="#" class="btn btn-primary">Read more <span class="icon icon-arrow-right"></span></a>
                </div>
              </article>
        }
    }
    

    Clearly I'm misunderstanding or not following the article properly.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Oct 08, 2018 @ 11:27
    Dave Woestenborghs
    100

    Hi Chris,

    I think the problem is that you want to show fields from the child items you are looping over, but your code wants to show them from the page your are current on (the overview)

    @Umbraco.Field("articleTitle")
    

    This code will try to render the property with alias "articleTitle" from the page you are currently on...so the overview page.

    What you need is :

    @Umbraco.Field(item, "articleTitle")
    

    That way it knows it has too look for the field on the current item in the loop.

    Or you could use :

    @item.GetPropertyValue<string>("articleTitle")
    

    Could you try that ?

    Dave

  • Chris Currie 4 posts 84 karma points
    Oct 08, 2018 @ 11:53
    Chris Currie
    0

    Fantastic. That's working. Much obliged.

    Interestingly,

    @Umbraco.Field(item, "articleTitle") 
    

    worked perfectly

    but

    @item.GetPropertyValue<string>("articleTitle") 
    

    caused a System.Web.HttpCompileException error:

    " error CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments"

    This confuses me because I had tried that approach and the Media Picker code segment I'm using does follow that format and works fine:

    @{
        var blogImage = item.GetPropertyValue<IPublishedContent>("articleImage");
        if (blogImage != null)
            {
                <img src="@blogImage.Url" width="871" height="326" alt="@blogImage.GetPropertyValue("alt")" />
            }
        }
    

    It'd be good to understand why that wasn't working... I'm also not sure why the articles seem to omit this.

    Great to be cracking on. Thanks again.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Oct 08, 2018 @ 11:58
    Dave Woestenborghs
    0

    Hi Chris,

    Glad to hear you got it working.

    Maybe you can create an issue for the documentation that the example code is not working correctly.

    The documentation is open source as well so people are constantly contributing to it as well.

    You could raise your issue here : https://github.com/umbraco/UmbracoDocs/issues

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft