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:
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.
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.
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)
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.
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 :-)
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:
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?
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?)
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.
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 ... :-)
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
}
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:
Instead of this:
And for UmbracoTemplatePage - when to use:
Instead of just:
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.
Hi Marrix
Allways use
And if you use ModelsBuilder use:
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
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):
And in my partial (used in homepage):
How to handle this?
Try to use in partial:
@inherits Umbraco.Web.Mvc.UmbracoViewPage
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
Probably the best explanation is this article - https://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/ by Dave Woestenborghs
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.
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
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
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:
My "homepage" Home.cshtml (child of master in Content) has this include:
A partialview CategoryList.cshtml - used in Home.cshtml - containing some category-items has this include:
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:
instead of just this:
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?
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?
Hi Nik,
HomePage and Master is connected like this:
And on my HomePage, I call the partial like this:
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.
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 ...?
I have another partial on Home, where I send the Model into the partial like this ...
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):
... the line commented out with // is what I prefer. And if not possible - I would like to understand why ... :-)
Marrix, you can use:
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
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
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
is working on a reply...