Getting Property value from the Homepage and displaying throughout in a partial view
Afternoon all,
I am completely new to Umbraco/MVC and I'm working on the following which I wounder if someone could give me a hand with? (using v7.13.2)
Each page on my site has the ability to change the background image of the header on that page, but I would like to add: If no image has been selected on a page, go to the homepage and use the image from there.
This is what I have so far:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
int imageId = Umbraco.AssignedContentItem.GetPropertyValue<int>("topBannerImage");
string topBannerImage = Umbraco.Media(imageId).Url;
if (string.IsNullOrWhiteSpace(topBannerImage))
{
var homePageImage = Model.Content.Parent.GetPropertyValue<IPublishedContent>("topBannerImage").Url;
topBannerImage = homePageImage;
}<header style="background-image:url('@topBannerImage')"></header>
The above works for picking a image per page, but not by going to the root level and getting the value from there if the Media picker is empty.
I have looked through forums/other sites and had no luck with this.
Thanks for this Alex. I have implemented my code as you have suggested, but the page is returning: "Object reference not set to an instance of an object.
Here is what I have in my partial view:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
int imageId = Umbraco.AssignedContentItem.GetPropertyValue<int>("topBannerImage");
string topBannerImage = imageId > 0 ? Umbraco.Media(imageId).Url : string.Empty;
if (string.IsNullOrWhiteSpace(topBannerImage))
{
var homePageImage = Model.Content.Site().GetPropertyValue<IPublishedContent>("topBannerImage").Url;
topBannerImage = homePageImage;
}
Getting Property value from the Homepage and displaying throughout in a partial view
Afternoon all,
I am completely new to Umbraco/MVC and I'm working on the following which I wounder if someone could give me a hand with? (using v7.13.2)
Each page on my site has the ability to change the background image of the header on that page, but I would like to add: If no image has been selected on a page, go to the homepage and use the image from there.
This is what I have so far:
The above works for picking a image per page, but not by going to the root level and getting the value from there if the Media picker is empty.
I have looked through forums/other sites and had no luck with this.
Can anyone help?
Hi David
Use this code:
This method:
Always looks at the root node, so it's what you need.
Thanks,
Alex
Thanks for this Alex. I have implemented my code as you have suggested, but the page is returning: "Object reference not set to an instance of an object.
Here is what I have in my partial view:
Hi David,
In which line exception?
Can you change :
Umbraco.TypedMedia instead of Umbraco.Media
Thanks,
Alex
Alex,
This is the line in question it is complaining about:
Hi David
Please, check what do you have in the root node in "topBannerImage" property.
There is should be a media item selected.
Thanks,
Alex
To avoid this exception you can use this code:
Thanks for the help on this Alex. Earlier today I managed to get this working using the following:
is working on a reply...