mauzilla
04/11/2024, 1:46 PMmauzilla
04/11/2024, 1:46 PMAndy Boot
04/11/2024, 2:14 PM<img src="@item.Content.Value<MediaWithCrops>("timelineimage").Url" class="img-fluid" />
But you could do this as part of your loop (if you use models builder):
@foreach(var item in Model.Items.Select(x => x.Content).OfType<MyTimelineItemModel>())
{
<img src="@item.Timelineimage.Url" class="img-fluid" />
}
(This assumes Model
is your page/block doc type, and Items
is your block list)
Hope this helps! πmauzilla
04/11/2024, 2:22 PMmauzilla
04/11/2024, 2:25 PMmauzilla
04/11/2024, 2:26 PMmauzilla
04/11/2024, 2:26 PMMatt Wise
04/11/2024, 2:55 PMhuwred
04/11/2024, 3:00 PMMatt Wise
04/11/2024, 3:00 PMtimelineItems
?Sebastiaan
04/11/2024, 3:03 PMcsharp
var x = 100;
mauzilla
04/11/2024, 3:07 PM@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
@using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
@using Umbraco.Cms.Core.Models.Blocks;
@{
var nodeId = int.Parse(ViewData["id"].ToString());
var node = Umbraco.Content(nodeId);
var timelineItems = node.Value<IEnumerable<BlockListItem>>("timelineItems");
}
mauzilla
04/11/2024, 3:08 PMmauzilla
04/11/2024, 3:08 PM@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
@using Umbraco.Cms.Web
@{
var modules = Model.Children;
}
@foreach (var module in modules)
{
var m = module.ContentType.Alias.ToLower();
var test = "123";
switch(module.ContentType.Alias.ToLower())
{
case "timeline":
@await Html.PartialAsync("~/Views/Partials/Modules/timeline.cshtml", null, new ViewDataDictionary(ViewData) { { "id", module.Id }})
break;
mauzilla
04/11/2024, 3:09 PMmauzilla
04/11/2024, 3:09 PMmauzilla
04/11/2024, 3:09 PM@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
@{
Layout = "master.cshtml";
}
@Html.Partial("rendering/render-modules")
mauzilla
04/11/2024, 3:10 PMmauzilla
04/12/2024, 10:04 AMmauzilla
04/12/2024, 10:04 AMmauzilla
04/12/2024, 10:05 AMmauzilla
04/12/2024, 10:05 AMmauzilla
04/12/2024, 10:11 AMmauzilla
04/12/2024, 10:11 AMAndy Boot
04/12/2024, 10:52 AMnodeId
and node
variables. I'm guessing the file you attached is for the whole page template? If so @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
should give you your Model
containing the equivalent to your old node
variable.
If you use models builder, you could strongly type key aspects of your code (as per my last message). But you could replace the first @inherits
line with something like @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<TimelinePage>
which would turn your Model
from IPublishedContent
to TimelinePage
(assuming TimelinePage is your pages doc type alias). That way you'd be able to access your doc type properties directly, rather than having to explicitly use the .Value("")
extension method each time.
However, if you don't use models builder I'd recommend for strings (or any other type) to use .Value<string>("alias")
. This way they're explicitly converted and parsed if the data allows.mauzilla
04/12/2024, 10:54 AMmauzilla
04/12/2024, 10:55 AMmauzilla
04/12/2024, 10:57 AMAndy Boot
04/12/2024, 10:58 AMmauzilla
04/12/2024, 10:59 AMmauzilla
04/12/2024, 10:59 AM@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
@{
Layout = "master.cshtml";
}
@Html.Partial("rendering/render-modules")
mauzilla
04/12/2024, 10:59 AMmauzilla
04/12/2024, 11:00 AM@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
@using Umbraco.Cms.Web
@{
var modules = Model.Children;
}
@foreach (var module in modules)
{
switch(module.ContentType.Alias.ToLower())
{
case "timeline":
@await Html.PartialAsync("~/Views/Partials/Modules/timeline.cshtml", null, new ViewDataDictionary(ViewData) { { "id", module.Id }})
break;
case "pageheader":
@await Html.PartialAsync("~/Views/Partials/Modules/page-header.cshtml", null, new ViewDataDictionary(ViewData) { { "id", module.Id }})
break;
case "pagevideo":
@await Html.PartialAsync("~/Views/Partials/Modules/page-video.cshtml", null, new ViewDataDictionary(ViewData) { { "id", module.Id }})
break;
case "genericsection":
@await Html.PartialAsync("~/Views/Partials/Modules/generic-section.cshtml", null, new ViewDataDictionary(ViewData) { { "id", module.Id }})
break;
mauzilla
04/12/2024, 11:00 AMmauzilla
04/12/2024, 11:00 AMmauzilla
04/12/2024, 11:00 AMmauzilla
04/12/2024, 11:02 AMmauzilla
04/12/2024, 11:03 AMmauzilla
04/12/2024, 11:04 AMmauzilla
04/12/2024, 11:04 AMmauzilla
04/12/2024, 11:05 AMmauzilla
04/12/2024, 11:06 AMmauzilla
04/12/2024, 11:06 AMAndy Boot
04/12/2024, 11:07 AMmodule
should still be an IPublishedContent
type which you could give to the 2nd parameter of the PartialAsync()
method, thus negating the need to provide a ViewDataDictionary
for the 3rd parameter. Your partial should then just be able to utilise the Model
.
But to be honest, I'd look at overhauling how this is structured. Personally I'd go through each doc type, create the necessary template, set as default, remove the current template, copy the partial code over to the new template (with perhaps some slight adapting). This would make it fall in line with a typical Umbraco setup and make it less confusing for future developers.mauzilla
04/12/2024, 11:09 AMAndy Boot
04/12/2024, 11:09 AMnode.Value<MediaWithCrops>("imageAlias")
to parse into something you can pull the URL frommauzilla
04/12/2024, 11:09 AMmauzilla
04/12/2024, 11:09 AMmauzilla
04/12/2024, 11:10 AMProBot β¨
04/12/2024, 11:10 AMAndy Boot
04/12/2024, 11:11 AMAndy Boot
04/12/2024, 11:11 AMMediaWithCrops
@using Umbraco.Cms.Core.Models
mauzilla
04/12/2024, 11:13 AMmauzilla
04/12/2024, 11:13 AMmauzilla
04/12/2024, 11:18 AMmauzilla
04/12/2024, 11:18 AMmauzilla
04/12/2024, 11:18 AM@inherits UmbracoViewPage<BlockListItem>
@using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
@using Umbraco.Cms.Core.Models.Blocks;
@using Umbraco.Cms.Core.Models;
@{
var nodeId = int.Parse(ViewData["id"].ToString());
var node = Umbraco.Content(nodeId);
var timelineItems = node.Value<IEnumerable<BlockListItem>>("timelineItems");
}
mauzilla
04/12/2024, 11:19 AMmauzilla
04/12/2024, 11:19 AMmauzilla
04/12/2024, 11:19 AMmauzilla
04/12/2024, 11:20 AMforeach (var item in timelineItems)
{
var timelineYearString = item.Content.Value("timelineyear").ToString(); // Convert to string
var timelineYear = DateTime.Parse(timelineYearString).Year;
var image = item.Content.Value<MediaWithCrops>.get("timelineimage");
....
mauzilla
04/12/2024, 11:21 AMmauzilla
04/12/2024, 11:21 AMmauzilla
04/12/2024, 11:21 AMAndy Boot
04/12/2024, 11:22 AMBlockListItem
which has Content
and Settings
. Both of these will have IPublishedElement
but should still contain the ability to do item.Content.Value()
.
Your code above for the image variable needs changing to:
var image = item.Content.Value<MediaWithCrops>("timelineimage");
(without the get()
)Andy Boot
04/12/2024, 11:23 AMAndy Boot
04/12/2024, 11:24 AMmauzilla
04/12/2024, 11:26 AMAndy Boot
04/12/2024, 11:27 AMimage.Url()
and you're winningmauzilla
04/12/2024, 11:29 AMmauzilla
04/12/2024, 11:29 AMmauzilla
04/12/2024, 11:29 AMAndy Boot
04/12/2024, 11:30 AMmauzilla
04/12/2024, 11:30 AMAndy Boot
04/12/2024, 11:34 AMmauzilla
04/12/2024, 11:36 AMmauzilla
04/12/2024, 11:36 AMAndy Boot
04/12/2024, 11:40 AM