danielhok
09/05/2023, 12:49 PMcsharp
@using Umbraco.Cms.Web.Common.PublishedModels;
@using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.Home>
@{
Layout = null;
}
...
@Html.Partial("Footer")
...
Not found
csharp
@using Umbraco.Cms.Web.Common.PublishedModels;
@using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.NotFound>
@{
Layout = null;
}
...
@Html.Partial("Footer")
...
Footer.cshtml Partial
csharp
@using Umbraco.Cms.Web.Common.PublishedModels;
@using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.Home>
<footer class="w-full absolute bottom-0 md:left-1/2 md:-translate-x-1/2 flex items-center justify-center text-white">© @DateTime.Now.Year - Made with<our-svg class="inline-block w-5 h-5 heart" media-item="@Model.FooterIcon"></our-svg> by Daniel Oberlechner</footer>
Can anyone guide me in the right direction to solve this problem?
I allready tried chatgtp but it wasn't really answering like I would excpect it.
Kind regards,
DanielOwain Jones
09/05/2023, 2:19 PMhtml
@model MyFooterViewModel
<footer class="w-full absolute bottom-0 md:left-1/2 md:-translate-x-1/2 flex items-center justify-center text-white">© @DateTime.Now.Year - Made with<our-svg class="inline-block w-5 h-5 heart" media-item="@Model.FooterIcon"></our-svg> by Daniel Oberlechner</footer>
FooterViewModel.cs:
csharp
public class MyFooterViewModel {
public string FooterIcon { get; set; }
public MyFooterViewModel (string footerIcon) {
FooterIcon = footerIcon;
}
}
Then in your Home.cshtml:
html
// ...
@ {
// Instantiatethe view model and give it data
var footerViewModel = new MyFooterViewModel(Model.FooterIcon);
}
// Render the partial
@Html.Partial("Footer", footerViewModel) // Synchronous way
@await Html.PartialAsync("Footer", footerViewModel) // Asynchronous way
<partial name="Footer" model="footerViewModel"/> // Tag helper way
// You can even instantiate and render it in one line if you want
@Html.Partial("Footer", new MyFooterViewModel(Model.FooterIcon))
// ...
Hope that helps!danielhok
09/05/2023, 2:33 PMOwain Jones
09/05/2023, 2:57 PM@inherits UmbracoViewPage
without the content type, that way the model that's passed to the partial will be of type IPublishedContent. (the interface all content models use)
And then you can use the .Value<objectType>("propertyAlias")
method to get the property data.
E.g.:
html
//@inherits UmbracoViewPage<ContentModels.Home>
@inherits UmbracoViewPage
@{
//var socialLinks = Model.FooterIcon;
var socialLinks = Model.Value<string>("footerIcon");
}
The only reason I would not recommend this way is because it is no longer strongly typed, and it will throw a null reference exception if the content model doesn't have that property. You could null check it using Model.HasValue("propertyAlias")
, but even then I would advise against it; as, if you rename/remove that property from your models in the future, you won't get any warnings and could potentially forget to update the aliases on this page. (from my experience, working with non strongly typed models is a pain)danielhok
09/05/2023, 3:00 PMOwain Jones
09/05/2023, 3:04 PMdanielhok
09/05/2023, 7:53 PMcsharp
Footer.cshtml
@using Umbraco.Cms.Web.Common.PublishedModels;
@using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
@using DanielOberlechner.Views.Partials
@model FooterViewModel
<footer class="w-full absolute bottom-0 md:left-1/2 md:-translate-x-1/2 flex items-center justify-center text-white">© @DateTime.Now.Year - Made with<our-svg class="inline-block w-5 h-5 heart" media-item="@Model.FooterIcon"></our-svg> by Daniel Oberlechner</footer>
FooterViewModel.cs
using Umbraco.Cms.Core.Models;
namespace DanielOberlechner.Views.Partials;
public class FooterViewModel
{
public MediaWithCrops FooterIcon { get; set; }
public FooterViewModel(MediaWithCrops footerIcon)
{
FooterIcon = footerIcon;
}
}
notFound.cshtml
@using Umbraco.Cms.Web.Common.PublishedModels;
@using DanielOberlechner.Views.Partials
@using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.NotFound>
...
@Html.Partial("Footer", new FooterViewModel(Model.FooterIcon))
...
Have you and hints were I made a mistake or where do I've to put the inherit statement that the view gets it's supposed to come from the home document type?danielhok
09/05/2023, 7:54 PMOwain Jones
09/05/2023, 7:56 PMModel.FooterIcon
in the notFound.cshtml
?Owain Jones
09/05/2023, 7:57 PMdanielhok
09/05/2023, 7:59 PMdanielhok
09/05/2023, 8:00 PMOwain Jones
09/05/2023, 8:15 PMNotFound
model doesn't contain the FooterIcon
property, you'll need to add that to your NotFound DocType in the backoffice.Owain Jones
09/05/2023, 8:15 PMcsharp
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.NotFound>
@{
var homePageNode = Model.Root<ContentModels.Home>(); // get the home page node
var footerIcon= homePageNode.FooterIcon; // get the footer icon, null check this
}
.Root()
will get the Ancestor (or self) of the current node(that is at level 1, aka the root of the content tree) .and the <ContentModels.Home>
will cast the IPublishedContent to the Home model. (I can't remember if this throws an exception if it's the wrong type)
But if your not found is not an ancestor of the Home page node (e.g. it is also at the root of the tree), then you can do something like this:
csharp
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.NotFound>
@{
var homePageNode = Model.SiblingsOfType(ContentModels.Home.ModelTypeAlias)?.FirstOrDefault() as ContentModels.Home;
var homePageNode2 = Model.Siblings<ContentModels.Home>()?.FirstOrDefault(); // I can't remember if this just casts, or gets all of the type
}
.Siblings()
will return all of the sibling nodes (the nodes that are at the same level / have the same parent as the current node)danielhok
09/05/2023, 8:19 PMdanielhok
09/05/2023, 8:20 PMOwain Jones
09/05/2023, 8:22 PMdanielhok
09/05/2023, 8:22 PMOwain Jones
09/05/2023, 8:24 PMdanielhok
09/05/2023, 8:27 PM