Copied to clipboard

Flag this post as spam?

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


  • Rabea 34 posts 186 karma points
    Apr 08, 2019 @ 12:15
    Rabea
    0

    Does not contain a definition for Content or typed content error in Version 8

    I was using this code in site layout in version 7 and this was working fine but now in version 8 this throws error in razor view: Can someone guide me about what i am doing wrong?

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        Layout = null;
        var home = Model.Content.AncestorOrSelf(1);
        var homepage = Umbraco.TypedContentAtRoot().First().DescendantsOrSelf().FirstOrDefault(x => x.DocumentTypeAlias == "homePage");
    }
    
  • Andrew 25 posts 148 karma points
    Apr 08, 2019 @ 14:31
    Andrew
    0

    Hi Rabea,

    What error are you getting?

    Are you just trying to get the homepage? if so the line you're using

    var home = Model.Content.AncestorOrSelf(1);
    

    Should work without the need of the second line. What is it you're trying to get?

    Looking at the answer and good explanation here it could be that because you're inheriting from a View Page you don't need .Content to access the Model property. So perhaps try var home = Model.AncestorOfSelf(1);

  • Corné Strijkert 80 posts 456 karma points c-trib
    Apr 08, 2019 @ 17:38
    Corné Strijkert
    0

    Getting the homepage by using

    var homepage = Umbraco.TypedContentAtRoot().First().DescendantsOrSelf().FirstOrDefault(x => x.DocumentTypeAlias == "homePage");
    

    is unnecessarily complex.

    1. The First() method can return null, then you will get a null reference exception. Also, when the node is not the first root child anymore, you will get unexpected results.
    2. DescendantsOrSelf() is far to heavy in your situation, because the homepage is always on one of the first levels in the tree. Using DescendantsOrSelf causes Umbraco to look to all the descendants.

    I would suggest you to use:

    @Model.AncestorOrSelf("homePage")
    

    Or better, with ModelsBuilder:

    @Model.AncestorOrSelf(HomePage.ModelTypeAlias)
    
  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Apr 08, 2019 @ 17:46
    Bjarne Fyrstenborg
    102

    Hi Rabea

    In Umbraco 8 the RenderModel has been removed, so it is no longer Model.Content just Model.

    Also note that some extensions methods have changed, e.g. .Site() is now called .Root()which basically just wraps Model.Content.AncestorOfSelf(1) (v7) and Model.AncestorOfSelf(1) (v8).

    I guess this should be sufficient to grap homepage node in most sites if the homepage node is on root level with other pages as children or descendants.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        Layout = null;
        var home = Model.Root();
    }
    

    Furthermore I think the property DocumentTypeAlias has been removed and you can access alias from IPublishedContent via .ContentType.Alias

    /Bjarne

  • Rabea 34 posts 186 karma points
    Apr 08, 2019 @ 18:05
    Rabea
    0

    Thanks a lot Bjarne, makes a lot more sense now

Please Sign in or register to post replies

Write your reply to:

Draft