Copied to clipboard

Flag this post as spam?

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


  • Mikael Axel Kleinwort 140 posts 484 karma points c-trib
    Apr 20, 2021 @ 19:42
    Mikael Axel Kleinwort
    0

    Checking for DataType present in nested blocklists from master viewfile

    I want to include the html for a modal popup at the end of the master view before the body closing tag, but only if a certain DataType or partial view is part of the completed page.

    Is there a way to accomplish this in Umbraco? How do I check whether a specific DataType or partial view is part of the page?

  • Brendan Rice 538 posts 1099 karma points
    Apr 20, 2021 @ 20:08
  • Mikael Axel Kleinwort 140 posts 484 karma points c-trib
    Apr 22, 2021 @ 11:39
    Mikael Axel Kleinwort
    0

    Hi Brendan,

    thank you for your suggestion. The thing is that I need to make sure I add the partial only once. I can have several instances of the DataType on a page, but the modal code should be included only once.

  • Brendan Rice 538 posts 1099 karma points
    Apr 22, 2021 @ 11:41
    Brendan Rice
    0

    Amir's solution is better :)

  • Amir Khan 1282 posts 2739 karma points
    Apr 20, 2021 @ 20:40
    Amir Khan
    1

    Do the datatypes all have the same alias on the document type? You could do a check like you would for an image if so.

    var propertyAlias = Model.Content.GetPropertyValue<IPublishedContent>("propertyAlias");
    if (propertyAlias != null) {
         add your modal here
    }
    

    Not sure on the checking if a partial view exists within a template is possible but you could render your mobile in that partial view I suppose?

  • Mikael Axel Kleinwort 140 posts 484 karma points c-trib
    Apr 22, 2021 @ 12:05
    Mikael Axel Kleinwort
    0

    Hi Amir,

    thank you for your suggestion. However the first code line does not work for me, I get this error:

    @using Umbraco.Core;
    @using Umbraco.Web;
    ...
    Error CS1061: 'IPublishedContent' does not contain a definition for 'GetPropertyValue' and no accessible extension method 'GetPropertyValue' accepting a first argument of type 'IPublishedContent' could be found (are you missing a using directive or an assembly reference?)    
    

    Any idea what I am doing wrong?

  • Brendan Rice 538 posts 1099 karma points
    Apr 22, 2021 @ 14:56
    Brendan Rice
    0
    var propertyAlias = Model.Content.Value("propertyAlias");
    
  • Mikael Axel Kleinwort 140 posts 484 karma points c-trib
    Apr 22, 2021 @ 15:01
    Mikael Axel Kleinwort
    0

    Thank you for all the input so far.

    I am on Umbraco 8. I have read that Model.Content.GetPropertyValue() doesn't work in V8 anymore.

    What works in Umbraco 8 is apparently Model.Value(). However, it seems that this only checks direct child properties of the model itself.

    What I need is checking whether a DataType is part of the rendered page anywhere. In my case, I have nested blocklists. I am looking for a way to find out whether a DataType is part of any of the blocklists on any level.

    I have changed the title of my question to make the issue clearer....

    What I have come up with as a solution so far is:

    • in the partial view rendering the DataType, a flag is set
    • in the master view, I check for the flag, and if it is set, the html for the modal is included

    This works, howeveer it feels more like brute-force and I wonder whether there is something more Umbraco'ish to solve this.

Please Sign in or register to post replies

Write your reply to:

Draft