Why should I use it? I have never used strongly typed models, but just used Model.Value("xxx"). But I guess there are reason to why models builder exist, i.e. that there are benefits from using it.
How should I use it? I have figured out how to use it, I think. It works, anyway, an renders the desired content.
Properties TestComp1 and TestComp2 comes from two separate compositions and TestComp1 is added to document type PageHome while both compositions have been added to PageDefault.
Then in template PageHome.cshtml I insert a partial view:
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<PageHome>
@{
var TestComp1 = Model.TestComp1?.ToString();
<p>@TestComp1</p>
}
This works fine, but if I insert it intro template PageDefault I get an error:
ModelBindingException: Cannot bind source content type
Umbraco.Cms.Web.Common.PublishedModels.PageDefault to model type
Umbraco.Cms.Web.Common.PublishedModels.PageHome.
And it makes sense, for sure. But is there no way that a partial can be used in templates where the model isn't sthe same? I seems inflexible to me since I don that often in the "old" way without models builder.
Or you can make ViewComponents instead of partials with your own created Model just for your needs for that View.
As to being a big pro for ModelsBuilder:
ModelsBuilder generates strongly typed models based on your Umbraco document types, ensuring that you have compile-time safety and intellisense support. This means you can access properties and content values with confidence, reducing the risk of runtime errors.
Why, and then how to, use models builder?
My qustion falls in to parts:
Template PageHome.cshtml
Template PageDefault.cshtml
Properties TestComp1 and TestComp2 comes from two separate compositions and TestComp1 is added to document type PageHome while both compositions have been added to PageDefault.
Then in template PageHome.cshtml I insert a partial view:
This works fine, but if I insert it intro template PageDefault I get an error:
And it makes sense, for sure. But is there no way that a partial can be used in templates where the model isn't sthe same? I seems inflexible to me since I don that often in the "old" way without models builder.
You can do several things, like if you know the partial only contains data from a composition you can just use that as the model:
Umbraco.Cms.Web.Common.Views.UmbracoViewPage<IPageComposition>
Or you can make ViewComponents instead of partials with your own created Model just for your needs for that View.
As to being a big pro for ModelsBuilder:
ModelsBuilder generates strongly typed models based on your Umbraco document types, ensuring that you have compile-time safety and intellisense support. This means you can access properties and content values with confidence, reducing the risk of runtime errors.
Thanks! ViewComponents seem to be the way to solve what I was looking for. :)
is working on a reply...