Copied to clipboard

Flag this post as spam?

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


  • Michael Mégroz 20 posts 111 karma points
    Oct 16, 2016 @ 16:47
    Michael Mégroz
    0

    Cast to Definit Object

    I have a Master Template . In this a defined the heder and the Footer.

    The Footer and the Header are Content.

    This is my Master Template:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @{
        Layout = null;
    }
    <html>
    <head>
        <title>@Model.Content.Name</title>
    </head>
    <body>
        <header>
    
    
    
    @{Unull;
        var dd = UmbracoContext.Current.Application.Services.ContentService;
        var help = dd.GetById(1063);
    
        var Test="";
        Test = "Test";
    
            }
    
    
        </header>
        <div class="content">
            @RenderSection("Connten_Section");
    
        </div>
        <footer></footer>
    </body>
    
    </html>
    

    Now My Problem is. How I Get the Property from the Header. I loade the Header With

    var dd = UmbracoContext.Current.Application.Services.ContentService;
    var help = dd.GetById(1063);
    

    But Help is From IContect. And I need ContentModels.Header. How I can Cast this or get ContentModels.Header.

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Oct 16, 2016 @ 18:00
    Dennis Adolfi
    1

    Hi Michael. You should'nt use the ContentService to get content, since it queries the database instead of the content cache, which is not good for performance. Also it returns a IContent, which you mentioned. The ContentService should only be used for create/edit/delete operations.

    Instead, use the UmbracoHelper methods go fetch content, which returns a IPublishedContent, which you should be able to cast as a ContentModel.

    To use the Umbraco helper, simply type @Umbraco.TypedContent(1063) to get your content.

    Hope this was helpful! / Dennis

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Oct 16, 2016 @ 21:02
    Alex Skrypnyk
    100

    Hi guys,

    Is it right that ContentModels.Header is Models Builders class?

    Just want to add to Dennis' post that you can cast IPublishedContent object to ContentModels.Header just usual casting in c#

    var itemTyped = Umbraco.TypedContent(1063) as ContentModels.Header;
    

    Thanks,

    Alex

  • Michael Mégroz 20 posts 111 karma points
    Oct 17, 2016 @ 16:38
    Michael Mégroz
    0

    thank you for the Help

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Oct 17, 2016 @ 17:20
    Alex Skrypnyk
    0

    You are welcome, Michael

    Write more questions, have a nice evening!

Please Sign in or register to post replies

Write your reply to:

Draft