Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I am new to working with Umbraco.
I have a checkbox inside a document type.
The field is called Hero Image Filter and it's intended purpose will allow the user to add a preset filter to the hero image, or remove it.
The checkbox is selected, and returns a value of True when the following command is placed inside the template
@Umbraco.Field("homeheroImageFilter")
When I create a variable as follows, it still returns the correct value
@{var filter = Umbraco.Field("homeheroImageFilter");} @filter
Now I want to use the variable to create an if statement, where if the condition is true it shows a
tag and content.
This returns a compilation error
@{var filter = Umbraco.Field("homeheroImageFilter");} @filter @if(filter == "True"){<p>Test</p>}
This also returns a compilation error
@{var filter = Umbraco.Field("homeheroImageFilter");} @filter @if(@filter == "True"){<p>Test</p>}
But when I change the if condition as follows, it works as expected..
@if("True" == "True"){<p>Test</p>}
So, what am I doing wrong? All help would be appreciated!
Hi Joshua, and welcome to Umbraco :)
Have a look here how to work with the true / false
https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/True-False
Hope this helps,
/Dennis
Hi Dennis,
Thanks for the link, but can you help me modify this code to display the value I need returning?
Thanks
J
Hi Joshua and also welcome to the Our forum :)
You need to change this
@{var filter = Umbraco.Field("homeheroImageFilter");} @if(filter == "True") { <p>Test</p> }
Into this and then it should work for you
@{var filter = Umbraco.Field("homeheroImageFilter");} @if(filter.ToString() == "True"){ <p>Test</p> }
Here you make sture to convert whatever the type of value you get back in your variable to a string and therefore you are now comparing two strings and won't see the yellow screen of death.
I hope this makes sense?
/Jan
Hi Joshua,
Here is a piece of code that you can use. So if the checkbox is checked then you will get the value printed out.
@if (Model.Content.GetPropertyValue<bool>("homeheroImageFilter")){ { <p>@Model.Content.Name</p> } }
A little explanation. Model.Content is refer to the page that you are viewing in the browser. Its the same when you are using the
So make sure that the page that you are viewing in the browser has the property on the page. Else you will need to do some traversing.
Thanks guys!
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Checkbox True False If Statement
I am new to working with Umbraco.
I have a checkbox inside a document type.
The field is called Hero Image Filter and it's intended purpose will allow the user to add a preset filter to the hero image, or remove it.
The checkbox is selected, and returns a value of True when the following command is placed inside the template
When I create a variable as follows, it still returns the correct value
Now I want to use the variable to create an if statement, where if the condition is true it shows a
tag and content.
This returns a compilation error
This also returns a compilation error
But when I change the if condition as follows, it works as expected..
So, what am I doing wrong? All help would be appreciated!
Hi Joshua, and welcome to Umbraco :)
Have a look here how to work with the true / false
https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/True-False
Hope this helps,
/Dennis
Hi Dennis,
Thanks for the link, but can you help me modify this code to display the value I need returning?
Thanks
J
Hi Joshua and also welcome to the Our forum :)
You need to change this
Into this and then it should work for you
Here you make sture to convert whatever the type of value you get back in your variable to a string and therefore you are now comparing two strings and won't see the yellow screen of death.
I hope this makes sense?
/Jan
Hi Joshua,
Here is a piece of code that you can use. So if the checkbox is checked then you will get the value printed out.
A little explanation. Model.Content is refer to the page that you are viewing in the browser. Its the same when you are using the
So make sure that the page that you are viewing in the browser has the property on the page. Else you will need to do some traversing.
Hope this helps,
/Dennis
Thanks guys!
is working on a reply...