I have a silly question that is as much a simple RAZOR / C# question as an Umbraco one, but I've looked for answers for quite some time and can't seem to find any.
My goal is to:
Test to see if a field in Umbraco has been provided... let's say check to see if an image has been provided via the media picker.
Throughout the view, display content when it has been provided and skip the block when it hasn't.
This is easy when one can live within an IF block, for example:
However, in complex layouts, let's say when the presence of that image changes the layout throughout the page, this method becomes terribly ugly.
What I'd like to do is instantiate myImage early and then easily test later if it has a value.
Here's what I've explored and rejected:
if (Model.Content.HasValue("myImage") {
iPublishedContent myImage = Umbraco.TypedMedia(Model.Content.GetProperValue("myImage"));
}
if (myImage!=null) {
<div>
<img src="@myImage.GetPropertyValue("umbracoFile")"> //etc.
</div>
}
This doesn't work as myImage looses scope when leaving the IF block (as expected)...
IPublishedContent? myImage;
if (Model.Content.HasValue("myImage") {
iPublishedContent myImage = Umbraco.TypedMedia(Model.Content.GetProperValue("myImage"));
}
if (myImage!=null) {
<div>
<img src="@myImage.GetPropertyValue("umbracoFile")"> //etc.
</div>
}
Doesn't work because IPublishedContent isn't nullable.
IPublishedContent myImage;
if (Model.Content.HasValue("myImage") {
iPublishedContent myImage = Umbraco.TypedMedia(Model.Content.GetProperValue("myImage"));
}
if (myImage!=null) {
<div>
<img src="@myImage.GetPropertyValue("umbracoFile")"> //etc.
</div>
}
Returns a 'Use of unassigned local variable' error.
IPublishedContent isn't nullable, you're right, but it's value can be null.
So something like this should work:
IPublishedContent myImage = null;
if (Model.Content.HasValue("myImage")) {
// assign to myImage
}
if (myImage != null) {
// do stuff
}
// more stuff
if (myImage != null) {
// do other stuff
}
Hopefully simple Umbraco / C# / RAZOR question...
I have a silly question that is as much a simple RAZOR / C# question as an Umbraco one, but I've looked for answers for quite some time and can't seem to find any.
My goal is to:
This is easy when one can live within an IF block, for example:
However, in complex layouts, let's say when the presence of that image changes the layout throughout the page, this method becomes terribly ugly. What I'd like to do is instantiate myImage early and then easily test later if it has a value.
Here's what I've explored and rejected:
This doesn't work as myImage looses scope when leaving the IF block (as expected)...
Doesn't work because IPublishedContent isn't nullable.
Returns a 'Use of unassigned local variable' error.
Same error... "Use of unassigned local variable"
Any ideas or tricks to accomplish this?
Thanks again for your time!
Brian
Hi Brian
IPublishedContent isn't nullable, you're right, but it's value can be null.
So something like this should work:
Thank you very much! I suspected it was a simple fix. I had a misunderstanding on what "nullable" meant. :-)
I really appreciate it.
is working on a reply...