Hi, I'm new to Umbraco and Razor. I'm testing with Umbraco 4.7.2.
I'm trying to see if a field has a value and checkbox is checked. "myLink" is a textstring and "newWindow" is a true/false. Basically, I'm trying to see if the link should be opened in a new window. I know you can do this easily in the WYSIWYG editor, however, I'm trying to build some custom functionality for an image rotator.
Yes as far as I know @Model.HasValue("someField") is the recommended way in Razor to check for a value in a field, as it will also handle a null reference too (if the field is not found).
Re your if statement, you could try something like this:
Thanks for the reply Barry! I'm still having some trouble. I've tested the true/false field and have seen both "0/1" or "False/True" as the value. Currently, its showing as "True" I tried to modify your code to use "True" but it's throwing an error. Any suggestions? I really appreciate the help!
@{ var linkTarget =(Model.newWindow =="True")?" target=\"_blank\"":""; <a href="@Model.myLink"@linkTarget>@Model.myLink</a> }
Razor If Statements
Hi, I'm new to Umbraco and Razor. I'm testing with Umbraco 4.7.2.
I'm trying to see if a field has a value and checkbox is checked. "myLink" is a textstring and "newWindow" is a true/false. Basically, I'm trying to see if the link should be opened in a new window. I know you can do this easily in the WYSIWYG editor, however, I'm trying to build some custom functionality for an image rotator.
While I'm asking, if this the best way to check if a field has text?
Thanks your input!
Hi Ryan, welcome to the forum!
Yes as far as I know @Model.HasValue("someField") is the recommended way in Razor to check for a value in a field, as it will also handle a null reference too (if the field is not found).
Re your if statement, you could try something like this:
Good luck!
Thanks for the reply Barry! I'm still having some trouble. I've tested the true/false field and have seen both "0/1" or "False/True" as the value. Currently, its showing as "True" I tried to modify your code to use "True" but it's throwing an error. Any suggestions? I really appreciate the help!
A true/false field should convert to an actual boolean, so you can do:
Thank you Dan! That worked.
I did have another if statement question. How do you check if a media picker field has a value?
Thanks again. The community here is great!
In 4.7.2 probably the safest way is to use
You can also do
is working on a reply...