How to write a conditional statement with empty property alias
What is the recommended approach for writing a conditional statement with a property alias? For example, if you have a text string property alias called propertyAlias and you want to do nothing if the property alias has no text, but do something else if the property alias has text, what would be the approach?
if it was a multimedia picker... basically something called a PropertyValueConverter in the core of Umbraco (you can write your own too for custom property editor types) is doing the conversion from the stored value to something nice to work with in your view.
When you just use
@Model.Value("title")
then the type returned is 'object' - if all you are doing is writing it out directly into the View, then there is no difference between
@Model.Value("title")
and
@(Model.Value<string>("title"))
but if you are assigning the value to a variable to manipulate it further, or you have a complex type (eg the media picker scenario) then the 'strongly typed' approach can save effort...
... why the extra brackets?
well because the strongly typed <string> bit looks like a html tag.. it can confuse the razor view syntax checker, putting it inside a @() just tells the parser that this is code, and not html...
How to write a conditional statement with empty property alias
What is the recommended approach for writing a conditional statement with a property alias? For example, if you have a text string property alias called
propertyAlias
and you want to do nothing if the property alias has no text, but do something else if the property alias has text, what would be the approach?Hi Bobi
An IPublishedContent item has a 'HasValue' method that you can check first before doing something with the property value:
is that what you are after?
regards
Marc
I think so. I will get back to you to mark this as the solution if it works. Just implementing now.
Hi, Marc, was there a reason you didn't use @Model.Value("string") instead of @(Model.Value
Hi Bobi
Yes, largely out of habit
if you write
You are requesting a 'strongly typed version' of the property, in this case a string... but if it were a 'Media Picker'
you would get back an IPublishedContent object representing the picked media
or
if it was a multimedia picker... basically something called a PropertyValueConverter in the core of Umbraco (you can write your own too for custom property editor types) is doing the conversion from the stored value to something nice to work with in your view.
When you just use
@Model.Value("title")
then the type returned is 'object' - if all you are doing is writing it out directly into the View, then there is no difference between
and
but if you are assigning the value to a variable to manipulate it further, or you have a complex type (eg the media picker scenario) then the 'strongly typed' approach can save effort...
... why the extra brackets?
well because the strongly typed
<string>
bit looks like a html tag.. it can confuse the razor view syntax checker, putting it inside a@()
just tells the parser that this is code, and not html...regards
Marc
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.