Copied to clipboard

Flag this post as spam?

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


  • Rasmus Trumf 78 posts 160 karma points
    Jan 03, 2021 @ 13:34
    Rasmus Trumf
    0

    Converting from U7 V7.15.4 to U8 V8.10.0

    Converting from U7 V7.15.4 to U8 V8.10.0

    I have some properties like CompanyName that is placed on the frontpage documenttype. It is edited only on the frontpage but the values are used on all pages.

    In _Layout.cshtml I have: var companyName = Umbraco.Field("companyName", recursive: true); wich works fine in U7. I have "@inherits Umbraco.Web.Mvc.UmbracoViewPage" at the top of _Layout.cshtml.

    I have in my new U8 site tried Model.Root() and Model.Value but i get an error saying: "IPublishedContent does not contain a definition for Root" and the same for Value. I use CurrentPage.Level and CurrentPage.Url and converted that to Model.Level and Model.Url wich works fine so Model is awailable just not Model.Root() and Model.Value.

    What to do please?

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Jan 07, 2021 @ 21:18
    Marc Goodson
    100

    Hi Rasmus

    Model.Root() and Model.Value

    https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Web/PublishedContentExtensions.cs

    which is Umbraco.Web

    So if you can put a @using Umbraco.Web statement at the top of your view that might give you the access you need to call .Root() and .Value()

    You would use the 'Fallback To' methods to use FallBack.ToAncestors to replace the recursive call you had in Umbraco.Field:

    https://our.umbraco.com/documentation/getting-started/design/Rendering-Content/#using-fall-back-methods

    regards

    Marc

  • Rasmus Trumf 78 posts 160 karma points
    Jan 07, 2021 @ 21:46
    Rasmus Trumf
    0

    Hi Marc

    Thanks a lot, that did the trick :)

    So Model.Root() is equivalent to Model.Content.AncestorOrSelf(1) right. And if I need documenttypealias "CompanyName" from FrontPage I do it like this?

    var frontpage = Model.Root();
    var companyname = frontpage<string>("CompanyName");
    

    As for the other it seems to work with

    Model.Value("PropertyName", fallback: Fallback.ToAncestors)
    

    But not with

    Model.Value<string>("PropertyName", fallback: Fallback.ToAncestors)
    

    Can't it be strongly typed?

    Thanks

  • Rasmus Trumf 78 posts 160 karma points
    Jan 07, 2021 @ 21:54
    Rasmus Trumf
    0

    Ahh this seems to work

    var companyName = Model.Root().Value<string>("companyName");
    

    Strange this does not...

    Model.Value<string>("PropertyName", fallback: Fallback.ToAncestors)
    

    Is it because it might be null if no ancestors has data?

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Jan 08, 2021 @ 08:41
    Marc Goodson
    0

    Hi Rasmus

    The fallback option that doesn't work

    does it error, or just not pull back the value from higher up the tree?

    If the compiler throws an error, then try wrapping the Model.Value part inside @() brackets like so:

    @(Model.Value<string>("PropertyName", fallback: Fallback.ToAncestors))
    

    if it's not erroring - but just not pulling the ancestor value from your root node, then obvious things are if the alias is different, or the node isn't under the root in the tree - less obvious is if one of the ancestor nodes between your page and the root, has a value, a space or some json markup that is not visible, but prevents the Fallback from going all the way up the tree.

    regards

    marc

  • Rasmus Trumf 78 posts 160 karma points
    Jan 08, 2021 @ 11:17
    Rasmus Trumf
    0

    Hi Marc

    This works:

    Model.Value("PropertyName", fallback: Fallback.ToAncestors)
    

    This does not:

    Model.Value<string>("PropertyName", fallback: Fallback.ToAncestors)
    

    because of the <string> and it is not supposed from the documentation. So everything is fine. https://our.umbraco.com/Documentation/Reference/Querying/IPublishedContent/Properties/#fallbacks

    I just wonder why it can not be strongly typed when using Fallback.ToAncestors when it can when not using Fallback or Fallback.ToDefaultValue.

Please Sign in or register to post replies

Write your reply to:

Draft