Copied to clipboard

Flag this post as spam?

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


  • Arnim Anhut 137 posts 148 karma points
    Jun 05, 2019 @ 09:58
    Arnim Anhut
    0

    Access custom property in Partial View Marco?

    Hello,

    I feel stupid - even after about an hour googeling I didn't find a solution to get access to my custom properties.

    My code is this:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var selection = Umbraco.Content(1073).Children.Where(x => x.IsVisible()).ToArray();
        foreach (var item in selection){
            <a href="@item.Url">@item.preis</a>
        }
    }
    

    But all I get is "'IPublishedContent' does not contain a definition for 'preis' and no accessible extension method 'preis' accepting a first argument of type 'IPublishedContent' could be found"

    How do I get access to my custom properties?

    Thanx, Arnim.

  • Arnim Anhut 137 posts 148 karma points
    Jun 05, 2019 @ 12:10
    Arnim Anhut
    0

    Aww ... right in front of my eyes ... shame on me ...

    Its just

    @item.Value("preis")
    

    Solved. 🙄

  • Arnim Anhut 137 posts 148 karma points
    Jun 06, 2019 @ 07:43
    Arnim Anhut
    100

    Now I really got it ... I guess?

    Umbraco.Content(xxx) always returns an strongly typed IPublishedContent that you have to cast to the desired DocumentType - Type (aka the name of the document type).

    My DocumentType is called "About", so instead of doing

    foreach (var item in selection){
    

    I have to do

    foreach (About item in selection){
    

    an now I can access "item.Preis".

    Another pitfall: The names in the backend are lowercase (i.e. "preis") but if you use them in code you have to go uppercase -> "Preis".

    I hope I got this all correct? Happy for help if I'm wrong!

Please Sign in or register to post replies

Write your reply to:

Draft