Copied to clipboard

Flag this post as spam?

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


  • Conor 4 posts 74 karma points notactivated
    Sep 15, 2017 @ 12:58
    Conor
    0

    Accessing a property's macro's fields

    Hey,

    I have an object of IPublishedContent called 'product'.

    I can access its 'productCategory' property as follows:

    GetPropertyValue<string>("productCategory")
    

    The product category is a value assigned by a dropdown menu used in the content page when creating the product page.

    I have another property, the product description, called 'productDescription' - which is a macro rather than a simple dropdown. This macro has two fields within it, a subheading and a description. I want to just access the description text field.

    So in pseudo, I'm imagining something like:

    GetPropertyValue<string>("productDescription").GetChildProperties("productDescription");
    

    I don't know what the syntax is or what functions I can use (GetChildProperties is just a made up example of what I'd like).

    Lastly, it may be useful to note that if I use the same code as I did for category but for description instead (seen below), the following output is produced. I would just want to access the description section of this.

    product.GetPropertyValue<string>("productDescription")
    
    // Produces output:
    
    <h2>Product Y Subheading</h2> <div class="zip-divder"></div<p>Product Y Description</p>
    

    Any help appreciated, cheers.

    Conor

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Sep 16, 2017 @ 09:38
    Marc Goodson
    0

    Hi Conor

    GetPropertyValue

    If you have a look at your Macro in the backoffice, you will see there are four ways of 'implementing it'.

    The current favoured approach is to use a PartialViewMacro, if this is the case in your scenario you should have a corresponding razor partial in PartialViewMacroFiles (Developer section), and in this file you should find the implementation that produces the markup

    <h2>Product Y Subheading</h2> <div class="zip-divder"></div<p>Product Y Description</p>
    

    If you are looking to change the output of the Macro, then update it here.

    You can access the Macro Parameters in this file using GetParameterValue from the Umbraco.Web.Models namespace eg

    @using Umbraco.Web.Models
    @{
    var mediaId = Model.GetParameterValue<string>("mediaId","somedefaultvalue");
    }
    

    Macros are designed to render out a particular layout for a set of parameters in the context of the page they are rendered on.

    So I have a feeling that if you have a scenario where you want to read the parameters entered into a Macro. outside of its associated Partial View Macro then using a Macro, might not be the best way to manage that content!

    Eg. I need to understand more about your scenario and what you are trying to output to know why product description needs to be in a Macro?

    regards Marc

Please Sign in or register to post replies

Write your reply to:

Draft