umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
Other
4.49k stars 2.69k forks source link

V8.7RC Block list rendering needs easier way to hook up models #8756

Closed skttl closed 4 years ago

skttl commented 4 years ago

In the first version of the block list, if you need to use ModelsBuilder models for your views, you need to write a bit of code in your view, like:

@using Umbraco.Core.Models.Block
@using Umbraco.Web.PublishedContentModels
@inherits UmbracoViewPage<BlockListItem>
@{
var feature = Model.Content as Feature;
var settings = Model.Settings as FeatureSettings
}

It would be much nicer, if you could handle the models in the @inherits statement, like

@inherits UmbracoViewPage<BlockListItem<Feature,FeatureSettings>>

This would result in Model.Content being Feature, and Model.Settings being FeatureSettings

Shazwazza commented 4 years ago

You can have partial views declared with the model you want like @model Feature and pass in Model.Content to render your Partial then there is no explicit casting since that is done implicitly by the partial view

skttl commented 4 years ago

I know, but if you need both the settings and the content in your partial view, then you can't. I would like a way to have Model.Content and Model.Settings in a specific model.

Shazwazza commented 4 years ago

Can you use a tuple? like passing in (Model.Content, Model.Settings) and then have @model (Feature,FeatureSettings) ?

dawoe commented 4 years ago

Another option is to create a custom UmbracoViewPage that allows this.

This is was undocumented feature of ditto . https://github.com/umco/umbraco-ditto/blob/develop/src/Our.Umbraco.Ditto/Web/Mvc/DittoView.cs

So instead of doing @inherits UmbracoViewPage<MyModel> you would do @inherits UmbracoViewPage<DittoView>

So you could create a UmbracoBlockViewPage which exposes the content and the settings.

Dave

skttl commented 4 years ago

@Shazwazza nope, getting

Cannot bind source type System.ValueTuple`2[[Umbraco.Core.Models.PublishedContent.IPublishedElement, Umbraco.Core, Version=8.0.0.0, Culture=neutral, PublicKeyToken=null],[Umbraco.Core.Models.PublishedContent.IPublishedElement, Umbraco.Core, Version=8.0.0.0, Culture=neutral, PublicKeyToken=null]] to model type System.ValueTuple`2[[Umbraco.Web.PublishedModels.Feature, 87rc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Umbraco.Web.PublishedModels.Feature, 87rc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].

when doing

@foreach (var block in Model.Blocks)
    {
        @Html.Partial("BlockList/" + block.Content.ContentType.Alias, (block.Content, block.Settings))
    }

Partial view has

@inherits UmbracoViewPage<(Feature Content, Feature Settings)>
skttl commented 4 years ago

Something like UmbracoBlockViewPage<T1,T2> would work too :)

I guess then Model would be T1, and Settings would be T2

Shazwazza commented 4 years ago

Yeah was afraid of that, i'm sure there's a way to tell razor to know about tuples in it's web.config but I've never tried. Else you can use the non value tuple like Tuple<Content, Settings> but that's a bit uglier. Anyways, just providing info :)

skttl commented 4 years ago

Fixed in #8841