By first checking whether the sitelogo is null before trying to write out the Url property, will give you the avenue to investigate, eg is the page with the siteLogo published? is the image in the media section etc...
Also, is the SiteLogo on the top root node of the site? but this code is written out on all pages? in which case the SiteLogo property won't exist on other pages... which might be the cause of the error - the way to workaround this is to read the property recursively up the ancestors of the tree until the sitelogo property is found eg:
If I understand your follow up question correctly (I think the <> have been removed)
Then when you write out properties from Umbraco, say a text string and you use
@Model.Value("aliasofProperty")
Then Umbraco 'guesses' what type of property it is and writes out the value... for a text field this is ok, becaues the value stored is the text you want to display...
... for a picker, like a content picker or media picker, the value stored is actually just the unique Id of the item that has been picked... and you 'could' use this along with an Umbraco Helper to retrieve the full picked item and retrieve the url...
eg
var pickedImage = Umbraco.Media(Model.Value("aliasOfImagePicker"));
but you end up 'doing this' all over the place in a view, which might have multiple images, or indeed a property that stores it's data in a more complex way than just text or the id... and so... PropertyValueConverters were born!
These secretly hide away in the background, (you can create your own) and are wired up to work with certain property types, you trigger them when you ask for the content in a specific type, via the <T> brackets after Model.Value ... if there is a PropertyValueConverter than knows how to take the stored value for that property and turn it into the type you are requesting, it will do the magic for you... so
var pickedImage = Model.Value<IPublishedContent>("aliasofImagePicker");
has the same outcome of the above, because there is a PropertyValueConverter 'under the hood' which knows how to convert from the picked media id into the Media object represented by IPublishedContent - and hence has the .Url property...
For Multiple Media Picker you would use:
var pickedImages = Model.Value<IEnumerable<IPublishedContent>>("aliasofImagesPicker");
and for a Multi Url Picker something like this
var footerLinks = Model.Value<IEnumerable<Link>>("aliasofFooterLinksUrlPicker");
So hopefully makes it easier to work with in the view...
Get Image from Media Picker
Hi,
Please help.
I am getting error - Object reference not set to an instance of an object.' when using below code.
I am using Umbraco 8 and this code is on my contentpage.cshtml
Hi darzren
I'm wondering if something like the following might help work out what's going wrong here:
}
By first checking whether the sitelogo is null before trying to write out the Url property, will give you the avenue to investigate, eg is the page with the siteLogo published? is the image in the media section etc...
Also, is the SiteLogo on the top root node of the site? but this code is written out on all pages? in which case the SiteLogo property won't exist on other pages... which might be the cause of the error - the way to workaround this is to read the property recursively up the ancestors of the tree until the sitelogo property is found eg:
}
But hopefully this makes it easier to debug!
regards
Marc
Hi Marc,
Thanks. It is working now.
my logo not on my root node. It is on one of my content page. I will work on the home page later on and check for null as well
Btw, can you explain why this Model.Value
As for render text, i am using Model.Value("sitename") and it works.
Thanks.
Hi darzren
If I understand your follow up question correctly (I think the
<>
have been removed)Then when you write out properties from Umbraco, say a text string and you use
Then Umbraco 'guesses' what type of property it is and writes out the value... for a text field this is ok, becaues the value stored is the text you want to display...
... for a picker, like a content picker or media picker, the value stored is actually just the unique Id of the item that has been picked... and you 'could' use this along with an Umbraco Helper to retrieve the full picked item and retrieve the url...
eg
but you end up 'doing this' all over the place in a view, which might have multiple images, or indeed a property that stores it's data in a more complex way than just text or the id... and so... PropertyValueConverters were born!
These secretly hide away in the background, (you can create your own) and are wired up to work with certain property types, you trigger them when you ask for the content in a specific type, via the
<T>
brackets after Model.Value ... if there is a PropertyValueConverter than knows how to take the stored value for that property and turn it into the type you are requesting, it will do the magic for you... sohas the same outcome of the above, because there is a PropertyValueConverter 'under the hood' which knows how to convert from the picked media id into the Media object represented by IPublishedContent - and hence has the .Url property...
For Multiple Media Picker you would use:
and for a Multi Url Picker something like this
So hopefully makes it easier to work with in the view...
if that was your question :-)
regards
Marc
Thanks this helped allot for Umbraco 9.
For anyone who has created a partial and wants to simply get the url of the media picker:
In your partial:
@{
var desktopImage = Model.Value
}
You also just want to check if your media picker has an image like this:
var desktopImage = Model.Value
Look for a url if it doesn't exist set your variable to an empty string otherwise your page will not render..
is working on a reply...