Copied to clipboard

Flag this post as spam?

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


  • Carlos Mosqueda 244 posts 435 karma points
    Mar 09, 2020 @ 17:53
    Carlos Mosqueda
    0

    How to get determine the type in Nested Content

    I got stuck on this so just some general information.

    I needed to determine the type of what the Nested Content elements were. And based on the Umbraco 8 documentation for Nested Content, it was not clear, because I have multiple different types of nested content with different property values. That said, here is my solution.

    It got the type by using ContentType.Alias

    @{
     var items = Model.Value<IEnumerable<IPublishedElement>>("homepageSections");
    
      foreach (var item in items)
      {
    
        if (item.ContentType.Alias == "homepageArticles"){
    
          //Show selected articles
    
        }else if (item.ContentType.Alias == "homepageEvents"){
    
            //Show selected articles
    
        }else{}
    
      }@*End foreach*@
    
    }
    
  • Paul Wright (suedeapple) 277 posts 704 karma points
    Mar 09, 2020 @ 23:10
    Paul Wright (suedeapple)
    0
    switch (item.ContentType.Alias)
        {
    
            case "homepageArticles":
                @Html.Partial("Modules/Articles",item)
                break;
    
            case "homepageEvents":
                @Html.Partial("Modules/Events", item)
                break;
    
                default:
                break;
        }
    
  • Carlos Mosqueda 244 posts 435 karma points
    Mar 12, 2020 @ 17:39
    Carlos Mosqueda
    0

    I am going to add onto this.

    I saw Paul Wright added the switch AND the 'item' being passed into the partial.

    Of course the switch should be a cleaner choice.

    Being that the example I originally had uses 'IPublishedElement' if you try to access the 'item' in the partial and you head looks something like this in your Partial

     @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    This this will not work because 'IPublishedElement' is not 'IPublishedContent' which is the error you will get if you try to use your partial.

    That said in your partial view, you would get rid of or comment out the line

     @inherits Umbraco.Web.Mvc.UmbracoViewPage 
    

    And instead use

      @model IPublishedElement
    

    Then your partial could look something like

     @model IPublishedElement
     @{
      var item = Model;
      <p>@item.Value("PageTitle")</p>
       or 
        <p>@Model.Value("PageTitle")
       @*so on and so forth*@
     }
    

    Since I am fairly new to the ways of the Umbraco 8, I thought I'd share some knowledge.

  • Carlos Mosqueda 244 posts 435 karma points
    Mar 16, 2020 @ 21:50
    Carlos Mosqueda
    0

    @Paul Wright or any other Umbracian

    Now that I am in the partialview using the IPublishedElement as the Model. I had to remove the

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    Now that I did that, I keep not being able to get to the root node.

    When I try to use

    Umbraco.ContentAtRoot().FirstOrDefault().Descendants().Where(x => x.ContentType.Alias == "PostDetailPage");
    

    I get an error that Umbraco has no context. It says ContentAtRoot does not exist in Umbraco.

    When I try to dot noate in Visual Studio, I just get the ".Web, .Mvc, .Core"

    How would I get back out to the root node. I have a part of the code that needs to exist in that partial to get to the root node.

  • Carlos Mosqueda 244 posts 435 karma points
    Mar 16, 2020 @ 22:21
    Carlos Mosqueda
    0

    Found my solution.

    Instead of using

    @model IPublishedElement  
    

    You use.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbraco.Core.Models.PublishedContent.IPublishedElement>
    
Please Sign in or register to post replies

Write your reply to:

Draft