Copied to clipboard

Flag this post as spam?

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


  • Marrix 24 posts 156 karma points
    Jul 13, 2017 @ 15:08
    Marrix
    0

    UmbracoTemplatePage and UmbracoViewPage

    Hi there

    I have seen the tutorials on Umbraco.tv - Working with Models Builder. But I am a little confused - because of the different ways to use the models. E.g.

    When to use UmbracoViewPage and when to use UmbracoTemplatePage.

    And when to use UmbracoViewPage - without the generic class/model like this:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage 
    

    Instead of this:

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

    And for UmbracoTemplatePage - when to use:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.someclass>
    

    Instead of just:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    

    I've seen so many combinations that it confuses me. A simple overview and some best practice would help me a lot. Please. And sorry for the inconvenience ...

    BTW: I understand that DynamicPublishedContent/CurrentPage will be removed in v.8 - therefore I will prefer not to use this.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 13, 2017 @ 15:12
    Alex Skrypnyk
    3

    Hi Marrix

    Allways use

    @inherits Umbraco.Web.Mvc.UmbracoViewPage 
    

    And if you use ModelsBuilder use:

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

    You don't need UmbracoTemplatePage with latest Umbraco, it contains the same methods but with Dynamics support.

    UmbracoTemplatePage is mostly a convenience, it is the same as UmbracoViewPage

    Really great explanations here - https://our.umbraco.org/forum/developers/api-questions/52597-What-is-the-difference-between-UmbracoViewPage-and-UmbracoTemplatePage

    Thanks,

    Alex

  • Marrix 24 posts 156 karma points
    Jul 13, 2017 @ 15:52
    Marrix
    0

    Hi Alex. Thanks for link and explanation.

    My problem is, that I - like you suggested - changed to UmbracoViewPage in my test-webproject. But it gave me some problems, when a viewpage is containing a partialview - if both view and partial contains UmbracoViewPage with a model, it threw an error. E.g:

    Cannot bind source content type Umbraco.Web.PublishedContentModels.HomePage to model type Umbraco.Web.PublishedContentModels.ProductOverview.

    Then why this?

    In my homepage (child of a master-page):

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

    And in my partial (used in homepage):

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

    How to handle this?

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 13, 2017 @ 16:05
    Alex Skrypnyk
    0

    Try to use in partial:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage

  • Søren Kottal 702 posts 4497 karma points MVP 5x c-trib
    Jul 13, 2017 @ 18:33
    Søren Kottal
    1

    That's because the "parent view" sends it's own model to the partial view. And the partial view can't see the model as ProductOverview because it is HomePage.

    Is ProductOverview a composition on your HomePage doctype? Then you can use @inherits UmbracoViewPage

    var productOverview = Umbraco.TypedContent(123);
    @Html.Partial("yourPartialView", productOverview)
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 13, 2017 @ 15:38
    Alex Skrypnyk
    2

    Probably the best explanation is this article - https://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/ by Dave Woestenborghs

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jul 13, 2017 @ 16:04
    Nik
    1

    Personally, I always try to use the UmbracoTemplatePage in my views. However if I require a model that isn't based on a render model, then I resort to the UmbracoViewPage. This is normally in partial views rather than main templates.

    With the introduction of Models Builder, unless my partial is handling a variety of IPublishedContent, I always specify the model in the type section for the UmbracoTemplatePage.

    UmbracoTemplatePage has a default model that contains an IPublishedContent which is accessed via Model.Content, however when you've specified a type Model.Content is of that type instead.

    In UmbracoViewPage Model is the type you specify, I'm not sure what it defaults to if you don't specify a type with it as I don't think I've used it in that way before.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 13, 2017 @ 16:08
    Alex Skrypnyk
    1

    Hi Nik

    When you are using UmbracoTemplatePage - Model.Content is current page

    When you are using UmbracoViewPage - Model is current page

    UmbracoViewPage is lighter :) less code

    Thanks,

    Alex

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jul 13, 2017 @ 16:13
    Nik
    0

    Hi Alex,

    As I understand it (and based on how it works in my experience), Model.Content is only the current page if it is in the template, or you've used it in a partial view without passing in a model. However if you are passing in a model to the partial view, that inherits from IPublishedContent (such as a models builder model) then in that partial, Model.Content is the new model.

    Personally I've had more conflicts with code behaviour with UmbracoViewPage than I have with UmbracoTemplatePage but there are times when I switch. Normally when I'm using a model that has nothing to do with IPublishedContent :-)

    It's interesting to see someone pushing the UmbracoViewPage as the default to use, just thought I'd share :-)

    Nik

  • Marrix 24 posts 156 karma points
    Jul 13, 2017 @ 17:00
    Marrix
    0

    Thanks, both of you :-)

    For now, I'm trying to understand UmbracoViewPage and how it handles the generic models across the different "layers". E.g. I have tried this:

    Master.cshtml (containing head, header, main-menu, footer) - not allowed in root, but allows almost all other views as a child. And it has this include:

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

    My "homepage" Home.cshtml (child of master in Content) has this include:

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

    A partialview CategoryList.cshtml - used in Home.cshtml - containing some category-items has this include:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    Q 1:

    I have added @Model.Name to both my Master, Home and partial. They all return the text/name "Home". It makes sense for Home and partial. But my Master has no Name and I did expect some error. Why is its @Model.Name returning/"inheriting" the name of its child - the Home-page - but is still able to handle its own properties like @Model.Description as well?

    Q 2: When my partial is including this:

    @* ProductOverview is the parent of all categories*@
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<ProductOverview>
    

    instead of just this:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    I get this error: Cannot bind source content type Umbraco.Web.PublishedContentModels.HomePage to model type Umbraco.Web.PublishedContentModels.ProductOverview

    Then how can I use a Model in a partial, when the containing page uses another Model? Is it not possible? Will I have to base a partials model on the containing views model?

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jul 13, 2017 @ 17:17
    Nik
    1

    Hi Marrix,

    With regards to Q1, does you Home Page inherit from your Master page? (i.e. when you created it have you ticked the Master page in the compositions section?)

    As for Q2, how are you calling your partial view?

  • Marrix 24 posts 156 karma points
    Jul 13, 2017 @ 19:11
    Marrix
    0

    Hi Nik,

    HomePage and Master is connected like this:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<HomePage>
    
    @{
        Layout = "Master.cshtml";
    
        //var backgroundImage = CurrentPage.coverPhoto;
        var backgroundImage = Model.CoverPhoto;
    }
    

    And on my HomePage, I call the partial like this:

        <div class="col-md-8">
            @Html.Partial("CategoryList");
        </div>
    
  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jul 13, 2017 @ 19:20
    Nik
    1

    Hi Marrix,

    What about the document types in the back office? How are they related?

    You need to get the product overview and then pass it into your partial view. At the minute the Partial view has no idea where it should get it's model from.

  • Marrix 24 posts 156 karma points
    Jul 13, 2017 @ 19:26
    Marrix
    0

    Hi Nik,

    In doc.type Home is "a child" of Master - and inherit the properties from there.

    Ahh ... I see :-) That explains it, I guess ...?

  • Marrix 24 posts 156 karma points
    Jul 13, 2017 @ 19:38
    Marrix
    0

    I have another partial on Home, where I send the Model into the partial like this ...

    @Html.Partial("AdvBoxLayout", @Model.ChooseAdvBox)
    

    I can do that, because ChooseAdvBox is a "content picker" property in Home-model. How can I enclose the ProductOverview-Model - which have no direct relationship to Home - from Home, where the Home-model rules, to the partial?

    In the partial I do this (as works):

    @* ProductOverview is the parent of all categories *@
    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    @{
        var selection = Model.FirstChild("productOverview").Children("category");
        // var selection = Model.Children("category"); --> only if inherits Umbraco.Web.Mvc.UmbracoViewPage<ProductOverview>
    }
    

    ... the line commented out with // is what I prefer. And if not possible - I would like to understand why ... :-)

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 13, 2017 @ 21:07
    Alex Skrypnyk
    1

    Marrix, you can use:

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

    In the view and in the partial view, but you have to pass HomePage Moderl when you call partial:

    @Html.Partial("AdvBoxLayout", Model)

    Then your commented code will work, but this partial will be only for HomePage docType.

    Thanks,

    Alex

  • Marrix 24 posts 156 karma points
    Jul 13, 2017 @ 21:18
    Marrix
    0

    Thanks, Alex.

    My "AdvBoxLayout" works fine - probably because it contains content from HomePage docType, as you explain.

    But if I understand you correctly, the other partial (containing content from Categories - which has nothing to do with the HomePage) has to be as it is now without a reference to its model

    @* ProductOverview is the parent of all categories*@
    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    @{
        var selection = Model.FirstChild("productOverview").Children("category");
        //var selection = Model.Children("category"); --> only if inherits Umbraco.Web.Mvc.UmbracoViewPage<ProductOverview> is possible
    }
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 13, 2017 @ 21:20
    Alex Skrypnyk
    100

    Yes, Marrix, if the partial view hasn't a relation to the current page, without model.

    You always can access Umbraco data with Umbraco Helper.

    Thanks,

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft