Hi all,
I'm having a hard time understanding the best way to code something clean with Umbraco. By that, I mean that a lot of code is in my templates which is not the cleanest work ever...
For example, my file "department.cshtml" starts with :
@using System.Runtime.InteropServices.JavaScript
@using Umbraco.Cms.Web.Common.PublishedModels; @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage @{ Layout = "master.cshtml"; ViewBag.Title = Model.Value("title");
IPublishedContent? mediaItem = Model.Value<IPublishedContent>("planImage");
string imageUrl = null;
if (mediaItem != null)
{
imageUrl = mediaItem.Url();
}
string? modelGuid = Model.Key.ToString();
IPublishedContent? team = Umbraco
.Content(Guid.Parse(modelGuid))?
.ChildrenOfType("people")?
.FirstOrDefault(x => x.IsVisible());
string? teamGuid = team?.Key.ToString();
IEnumerable<IPublishedContent>? teamMembers = null;
IEnumerable<IPublishedContent>? managers = null;
if (teamGuid != null) {
teamMembers = Umbraco.Content(Guid.Parse(teamGuid))?
.ChildrenOfType("person")?
.Where(x => x.IsVisible())
.Where(x => x.Value("manager")?.ToString() == "False")
.OrderBy(x => x.Value("lastName"));
managers = Umbraco.Content(Guid.Parse(teamGuid))?
.ChildrenOfType("person")?
.Where(x => x.IsVisible())
.Where(x => x.Value("manager")?.ToString() == "True")
.OrderBy(x => x.Value("lastName"));
}
All this code would normally go to the file code-behind "department.cs" attached to my Razor page. But, if I try to use this way, I can't use some code for Umbraco like :
Umbraco
.Content(Guid.Parse(modelGuid))?
.ChildrenOfType("people")?
.FirstOrDefault(x => x.IsVisible());
How can I do this ?
Thank you all in advance for your time and patience.
Disclaimer : I'm a baby dev currently doing her first project ever, sorry if all of this sounds stupid, I'm learning. ☺