Copied to clipboard

Flag this post as spam?

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


  • Esben Viborg 4 posts 84 karma points
    Jun 09, 2020 @ 19:06
    Esben Viborg
    0

    Umbraco 8 DocumentTypeAlias

    Hi

    In umbraco 7 i used the code below to include partials based on whatever nested content was chosen in a particular document type. However i am struggeling to figure out how to do the same on Umbraco 8.

    Is it possible to get the Document Type Alias of nested content in Umbraco 8?

    var modules = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("modules");
    foreach (var module in modules)
    {
        @Html.Partial("~/Views/Partials/Modules/" + module.DocumentTypeAlias + ".cshtml", module)
    }
    
  • Bjarne Fyrstenborg 1182 posts 3441 karma points MVP 4x c-trib
    Jun 09, 2020 @ 19:27
    Bjarne Fyrstenborg
    101

    Hi Esben

    Sure, in Umbraco 8 there is no longer a DocumentTypeAlias property on IPublishedContent, but just a ContentType property, which has an Alias property.

    So something like this should work:

    var modules = Model.Value<IEnumerable<IPublishedContent>>("modules");
    foreach (var module in modules)
    {
        @Html.Partial("~/Views/Partials/Modules/" + module.ContentType.Alias + ".cshtml", module)
    }
    

    If you are using Nested Content you're working with element types, which is based on IPublishedElement: https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Core/Models/PublishedContent/IPublishedElement.cs#L16

    where IPublishedContent inherit from IPublishedElement and therefore share the same based properties: https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Core/Models/PublishedContent/IPublishedContent.cs#L13

    So with Nested Content this should work.

    var modules = Model.Value<IEnumerable<IPublishedElement>>("modules");
    foreach (var module in modules)
    {
        @Html.Partial("~/Views/Partials/Modules/" + module.ContentType.Alias + ".cshtml", module)
    }
    

    /Bjarne

  • Esben Viborg 4 posts 84 karma points
    Jun 09, 2020 @ 23:07
    Esben Viborg
    0

    Thanks alot, that did the trick :D Some how i missed that property when going through the documentation.

    However i hit another snag i hope someone might be able to help with.

    After i got the partial included, i cant seem to get the "module" variabel to parse through to the partial. When i try i get an error telling me "The name 'module' does not exist in the current context"

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        Layout = "master.cshtml";
    }
    
    @* the fun starts here *@
    
        <div role="main" class="main">
            @{
                var modules = Model.Value<IEnumerable<IPublishedElement>>("contentclass");
                foreach (var module in modules)
                {
                    @Html.Partial("~/Views/Partials/Modules/" + module.ContentType.Alias + ".cshtml", module)
                }
            }
        </div>
    

    Partial Code:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    @{
        var logoref = module.Value<IEnumerable<IPublishedElement>>("LogoRefModuleClass");
    
    }
    
    <div class="container">
        <div class="row text-center pb-2 pt-3 mt-4 mb-3">
            <div class="owl-carousel owl-theme carousel-center-active-item" data-plugin-options="{'responsive': {'0': {'items': 1}, '476': {'items': 1}, '768': {'items': 5}, '992': {'items': 7}, '1200': {'items': 7}}, 'autoplay': true, 'autoplayTimeout': 1000, 'dots': false, 'margin': 20}">
    
                @foreach (var item in logoref)
                {
                    var image = item.Value<IPublishedContent>("logo");
    
                    <div>
                        <img class="img-fluid" src="@image.Url" alt="">
                    </div>
                }
    
            </div>
        </div>
    </div>
    
  • Søren Kottal 530 posts 3521 karma points MVP 2x c-trib
    Jun 10, 2020 @ 07:13
    Søren Kottal
    0

    Hi Esben

    The default model for UmbracoViewPage is IPublishedContent, so you have to specify that your model is IPublishedElement.

    Instead of writing @inherits Umbraco.Web.Mvc.UmbracoViewPage you can write @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedElement>

  • Esben Viborg 4 posts 84 karma points
    Jun 10, 2020 @ 15:09
    Esben Viborg
    0

    I have already tried that and unfortunatly the issue is the same with or without:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedElement>
    

    If i copy the code from the included partial into the main code instead of using a partial, everything works as expected.

  • Søren Kottal 530 posts 3521 karma points MVP 2x c-trib
    Jun 11, 2020 @ 07:02
    Søren Kottal
    0

    Ah sorry, didn't see you were using module in your partial view. It should be Model. In other words, instead of

    var logoref = module.Value<IEnumerable<IPublishedElement>>("LogoRefModuleClass");
    

    you should write

    var logoref = Model.Value<IEnumerable<IPublishedElement>>("LogoRefModuleClass");
    
  • Esben Viborg 4 posts 84 karma points
    Jun 18, 2020 @ 22:19
    Esben Viborg
    0

    ... i hadn't even seen that myself. Maybe its time to get glasses :D

    That solved the problem, Thanks alot for the help

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies